Tag: json deserialization

将某个JSON值映射到枚举值C#

我正在为Stack Exchange API创建类。 filter_object类型包含成员filter_type ,该成员将是safe , unsafe或invalid 。 所以我创建了一个这样的枚举: [JsonConverter(typeof(StringEnumConverter))] public enum FilterType { safe, @unsafe, // Here lies the problem. invalid } 由于unsafe是一个关键字,我不得不为它添加一些前缀。 但是,如何使值“不安全”自动映射到@unsafe ? 示例JSON: { “filter”: “….”, “filter_type”: “unsafe”, “included_fields”: [ “…”, “….”, “…..” ] } 如何反序列化,以便filter_type自动转换为FilterType.@unsafe ? 更新 – 解决: 在标识符之前使用@符号可以使其与关键字相同。 即使@出现在intellisense中,它也能正常工作。

将Json反序列化为对象和子对象以进行返回

还不太熟悉JSON,并且遇到了一个对我来说不明显的问题。 我正在查询的api返回一个标准的响应对象,其中处理的命令/ api请求的结果嵌入在json中的数据对象中。 因此,对于API上的所有请求,响应如下所示,数据组件的变化取决于所请求的内容。 ObjectType1响应 { “data”: { “person” : { “id” : 21, “name” : “Json can be annoying at times” } }, “message” : “”, “result” : “success” } 或者关于api的其他请求将返回以下列表 ObjectType2响应 { “data”: { “1234” : { “id”: 1234, “title” : “Terminator” }, “3245” : { “id” : 3245, “name” : “Terminator 2” […]

读取特定JSON字符串时出现对象引用错误?

我已经使用JSON到C#类转换器 ,它生成以下类: JSON {“ios_info”:{“serialNumber”:”F2LLMBNJFFF”,”imeiNumber”:”01388400413235″,”meid”:””,”iccID”:”8901410427640096045″,”firstUnbrickDate”:”11\/27\/13″,”lastUnbrickDate”:”11\/27\/13″,”unbricked”:”true”,”unlocked”:”false”,”productVersion”:”7.1.2″,”initialActivationPolicyID”:”23″,”initialActivationPolicyDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”appliedActivationPolicyID”:”23″,”appliedActivationDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”nextTetherPolicyID”:”23″,”nextTetherPolicyDetails”:”US AT&T Puerto Rico and US Virgin Islands Activation Policy”,”macAddress”:”ACFDEC6C988A”,”bluetoothMacAddress”:”AC:FD:EC:6C:98:8B”,”partDescription”:”IPHONE 5S SPACE GRAY 64GB-USA”},”fmi”:{“@attributes”:{“version”:”1″,”deviceCount”:”1″},”fmipLockStatusDevice”:{“@attributes”:{“serial”:”F2LLMBNJFFFQ”,”imei”:”013884004132355″,”isLocked”:”true”,”isLost”:”false”}}},”product_info”:{“serialNumber”:”F2LLMBNJFFF”,”warrantyStatus”:”Apple Limited Warranty”,”coverageEndDate”:”11\/25\/14″,”coverageStartDate”:”11\/26\/13″,”daysRemaining”:”497″,”estimatedPurchaseDate”:”11\/26\/13″,”purchaseCountry”:”United States”,”registrationDate”:”11\/26\/13″,”imageURL”:”http:\/\/service.info.apple.com\/parts\/service_parts\/na.gif”,”explodedViewURL”:”http:\/\/service.info.apple.com\/manuals-ssol.html”,”manualURL”:”http:\/\/service.info.apple.com\/manuals-ssol.html”,”productDescription”:”iPhone 5S”,”configDescription”:”IPHONE 5S GRAY 64GB GSM”,”slaGroupDescription”:””,”contractCoverageEndDate”:”11\/25\/15″,”contractCoverageStartDate”:”11\/26\/13″,”contractType”:”C1″,”laborCovered”:”Y”,”limitedWarranty”:”Y”,”partCovered”:”Y”,”notes”:”Covered by AppleCare+ – Incidents Available”,”acPlusFlag”:”Y”,”consumerLawInfo”:{“serviceType”:””,”popMandatory”:””,”allowedPartType”:””}}} 从JSON以上读取所有数据,但only the line at which i get […]

在C#中反序列化嵌套的JSON数组

我有一个带有嵌套对象的JSON数组,代表一个菜单,如下所示: [ [ { “name”: “Item 1”, “id”: 1 }, { “name”: “Item 2”, “id”: 2, “children”: [ [ { “name”: “Item 21”, “id”: 21 } ] ] }, { “name”: “Item 3”, “id”: 3, “children”: [ [ { “name”: “Item 31”, “id”: 31, “children”: [ [ { “name”: “Item 311”, “id”: 311 }, { […]

如何在WCF Rest Service中创建JSON以匹配/序列化为DataContract

接口: namespace SQRT_WCF { [DataContract] public class PlaceOrder { [DataMember] public string claimID { get; set; } [DataMember] public string rederenceDate { get; set; } } } 调用C#方法: public SQ_jsonModel.Response placeOrder(PlaceOrder argPlaceOrderJSON) { … } 我已将我的web服务附加到工作进程,并跟踪代码。 到目前为止,我尝试过的所有内容,变量argPlaceOrderJSON都为null。 我尝试过至少三种变化: 变化#1 { “claimID” : “value”, “rederenceDate” : “value” } 变化#2: { “PlaceOrder” : { “claimID” : “value”, […]

在C#中反序列化JSON数组

我遇到了一个棘手的问题。 我有一个这种格式的JSON字符串: [{ “record”: { “Name”: “Komal”, “Age”: 24, “Location”: “Siliguri” } }, { “record”: { “Name”: “Koena”, “Age”: 27, “Location”: “Barasat” } }, { “record”: { “Name”: “Kanan”, “Age”: 35, “Location”: “Uttarpara” } } … … ] “记录”中的字段可以增加或减少。 所以,我做了这样的课程: public class Person { public string Name; public string Age; } public class PersonList { […]