使用JSON反序列化JSON块和嵌套的嵌入对象数组.网络失败
本文关键字:JSON 对象 数组 网络 失败 反序列化 嵌套 使用 | 更新日期: 2023-09-27 18:17:42
下面是一个JSON响应从一个REST API调用我在Windows商店应用程序(Win 8.1)。它包含来自REST API调用的一页结果,每个结果(行)是一个PropertyMaster记录/对象。下面是Visual Studio 2013使用Paste Special as JSON IDE特性创建的JSON类。正如您所看到的,PropertyMaster对象有一个addresses字段,该字段是一个Address对象数组。如果您查看JSON,您将看到第一个结果在addresses JSON数组中确实有一个有效的Address对象。但是,当我使用以下调用反序列化JSON响应时:
JsonConvert.DeserializeObject<PropertyRecordsResponse>(json);
地址PropertyMaster对象中的字段为NULL。我没有得到JSON抛出的任何异常。. NET那么为什么不地址 JSON数组的内容被正确解析成地址对象的数组?任何关于如何跟踪和解决这个问题的建议都是值得赞赏的。
注意:我确实找到了这篇关于反序列化嵌套对象数组的文章:
用json数组的数组反序列化json字符串。净
但是我发誓我以前在JSON中有过嵌套的对象数组,并且不需要采取特殊的操作来反序列化它们。
JSON响应来自API调用
{
"page": {
"totalRows": 13
},
"records": [
{
"display": "14CAP-00000005",
"module": "Building",
"assignToInfo": {
"totalJobCost": "0.0"
},
"id": "DUB14-00000-0000I",
"type": {
"id": "Building-Mobile-Mobile-Tony",
"display": "Building/Mobile/Mobile/Tony"
},
"status": {
"id": "Ready.cto.cIssue",
"display": "Ready to Issue"
},
"openDate": "2014-08-07"
},
{
"display": "14CAP-00000006",
"module": "Building",
"assignToInfo": {
"totalJobCost": "0.0"
},
"id": "DUB14-00000-0000J",
"type": {
"id": "Building-Mobile-Mobile-Tony",
"display": "Building/Mobile/Mobile/Tony"
},
"status": {
"id": "Ready.cto.cIssue",
"display": "Ready to Issue"
},
"openDate": "2014-08-07"
},
{
"addresses": [
{
"id": "999974830-924597381",
"display": "233 Camino Ramon Suite 500, San Ramon, CA, 94583",
"houseNumber": "233",
"streetName": "Camino Ramon",
"unit": "500",
"unitType": "Suite",
"state": "CA",
"postalCode": "94583",
"city": "San Ramon",
"isPrimary": "True",
"enabled": true,
"entityState": "Unchanged"
}
]
}
]
}
VS2013为反序列化创建的类
public class PropertyRecordsResponse
{
public Page page { get; set; }
public PropertyMaster[] records { get; set; }
/// <summary>
/// Deserializes a JSON response from an Accela API call into a property records response object (and sub-objects).
/// </summary>
/// <param name="json">The JSON response in string form.</param>
public static PropertyRecordsResponse ParseJSON(string json)
{
return JsonConvert.DeserializeObject<PropertyRecordsResponse>(json);
}
}
/// <summary>
/// One page of results as returned by an Accela REST API method that returns property records.
/// </summary>
public class Page
{
public int totalRows { get; set; }
public bool hasMore { get; set; }
}
public class PropertyMaster
{
public Address[] addresses { get; set; }
public string display { get; set; }
public string module { get; set; }
public string name { get; set; }
public Assigntoinfo assignToInfo { get; set; }
public User user { get; set; }
public string createdDate { get; set; }
public string id { get; set; }
public string description { get; set; }
public bool isPrivate { get; set; }
public RecType type { get; set; }
public Status status { get; set; }
public Department department { get; set; }
public Staffperson staffPerson { get; set; }
public string assignDate { get; set; }
public string scheduleDate { get; set; }
public string scheduleTime { get; set; }
public string openDate { get; set; }
public string priority { get; set; }
public string shortNotes { get; set; }
public string templateName { get; set; }
}
public class Assigntoinfo
{
public string totalJobCost { get; set; }
public string completeDate { get; set; }
public string AssignDate { get; set; }
public string scheduledDate { get; set; }
public string assignStaff { get; set; }
public string assignDepartment { get; set; }
public string priority { get; set; }
}
public class User
{
public string firstName { get; set; }
public string lastName { get; set; }
public string civicId { get; set; }
public string email { get; set; }
}
public class RecType
{
public string id { get; set; }
public string display { get; set; }
public string module { get; set; }
public string group { get; set; }
public string subGroup { get; set; }
public string category { get; set; }
public string type { get; set; }
public string security { get; set; }
public string[] inspectionGroups { get; set; }
public string[] standardCommentsGroupIds { get; set; }
}
public class Status
{
public string id { get; set; }
public string display { get; set; }
}
public class Department
{
public string id { get; set; }
public string display { get; set; }
public string agency { get; set; }
public string bureau { get; set; }
public string division { get; set; }
public string section { get; set; }
public string group { get; set; }
public string subGroup { get; set; }
public string subGroupDescription { get; set; }
}
public class Staffperson
{
public string id { get; set; }
public string display { get; set; }
public string agencyCode { get; set; }
public string auditStatus { get; set; }
public string bureauCode { get; set; }
public string divisionCode { get; set; }
public string firstName { get; set; }
public string groupCode { get; set; }
public string lastName { get; set; }
public string officeCode { get; set; }
public string sectionCode { get; set; }
public string serviceProviderCode { get; set; }
public string userStatus { get; set; }
}
public class Address
{
public string id { get; set; }
public string display { get; set; }
public string addressFormat { get; set; }
public string houseNumber { get; set; }
public string houseNumberFraction { get; set; }
public string streetDirection { get; set; }
public string streetName { get; set; }
public string streetSuffix { get; set; }
public string streetSuffixDirection { get; set; }
public string unit { get; set; }
public string unitEnd { get; set; }
public string unitType { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
public string city { get; set; }
public string county { get; set; }
public string country { get; set; }
public string isPrimary { get; set; }
public string xCoordinate { get; set; }
public string yCoordinate { get; set; }
public bool enabled { get; set; }
public string entityState { get; set; }
}
看起来address
不在JSON中的正确位置。尝试反序列化这个(地址在第二条记录中):
{
"page":{
"totalRows":1
},
"records":[
{
"display":"14CAP-00000005",
"module":"Building",
"assignToInfo":{
"totalJobCost":"0.0"
},
"id":"DUB14-00000-0000I",
"type":{
"id":"Building-Mobile-Mobile-Tony",
"display":"Building/Mobile/Mobile/Tony"
},
"status":{
"id":"Ready.cto.cIssue",
"display":"Ready to Issue"
},
"openDate":"2014-08-07"
},
{
"display":"14CAP-00000006",
"module":"Building",
"assignToInfo":{
"totalJobCost":"0.0"
},
"id":"DUB14-00000-0000J",
"type":{
"id":"Building-Mobile-Mobile-Tony",
"display":"Building/Mobile/Mobile/Tony"
},
"status":{
"id":"Ready.cto.cIssue",
"display":"Ready to Issue"
},
"openDate":"2014-08-07",
"addresses":[
{
"id":"999974830-924597381",
"display":"233 Camino Ramon Suite 500, San Ramon, CA, 94583",
"houseNumber":"233",
"streetName":"Camino Ramon",
"unit":"500",
"unitType":"Suite",
"state":"CA",
"postalCode":"94583",
"city":"San Ramon",
"isPrimary":"True",
"enabled":true,
"entityState":"Unchanged"
}
]
}
]
}