JSON转C#对象时出现Object null的问题求助
JSON反序列化后对象全为null的排查方案
接口返回的JSON示例
[ { "id": 1, "firstName": "John", "lastName": "Smith", "company": { "id": 19297, "identifier": "XYZTestCompany", "name": "XYZ Test Company", "_info": { "company_href": "zzzzzzzzzzzz", "mobileGuid": "2685633f-6375-43" } }, "site": { "id": 1502, "name": "Main", "_info": { "site_href": "zzzzzzzzz" } }, "inactiveFlag": false, "title": "President", "marriedFlag": false, "childrenFlag": false, "unsubscribeFlag": false, "gender": "Male", "mobileGuid": "afghgfdfgh", "defaultPhoneType": "Direct", "defaultPhoneNbr": "00000000", "defaultBillingFlag": false, "defaultFlag": false, "companyLocation": { "id": 1, "name": "Company", "_info": { "location_href": "zzzzzzzz" } }, "communicationItems": [ { "id": 10, "type": { "id": 2, "name": "Name", "_info": { "type_href": "zzzzzzz" } }, "value": "8", "defaultFlag": true, "communicationType": "Phone" } ], "types": [ { "id": 2, "name": "Name", "_info": { "type_href": "zzzzzzzz" } } ], "customFields": [ { "id": 41, "caption": "Type", "type": "Text", "entryMethod": "List", "numberOfDecimals": 0 }, { "id": 42, "caption": "Synce", "type": "Checkbox", "entryMethod": "EntryField", "numberOfDecimals": 0 } ], "ignoreDuplicates": false, "_info": { "lastUpdated": "2015-10-03T05:59:47Z", "updatedBy": "Portal", "communications_href": "z", "notes_href": "z", "tracks_href": "z", "portalSecurity_href": "z", "activities_href": "z", "documents_href": "z", "configurations_href": "z", "tickets_href": "z", "opportunities_href": "z", "projects_href": "z", "image_href": "z" } } ]
自定义C#实体类
public class ContactJson { public List<Contact> Contact { get; set; } } public class Contact { public int id { get; set; } public string firstName { get; set; } public string lastName { get; set; } public Company company { get; set; } public string site { get; set; } public bool inactiveFlag { get; set; } public string title { get; set; } public bool marriedFlag { get; set; } public bool childrenFlag { get; set; } public bool unsubscrubeFlag { get; set; } public string gender { get; set; } public string mobileGuid { get; set; } public string defaultPhoneType { get; set; } public string defaultPhoneNbr { get; set; } public bool defaultbillingflag { get; set; } public bool defaultFlag { get; set; } public string companyLocation { get; set; } public string communicationItems { get; set; } public string types { get; set; } public string customFields { get; set; } public bool ignoreDuplicates { get; set; } public string info { get; set; } } public class Company { public int id { get; set; } public string name { get; set; } public string info { get; set; } }
反序列化代码
var ConnectwiseClient = new RestClient("https://api-aus.myconnectwise.net/v4_6_release/apis/3.0//company/contacts/"); var request = new RestRequest() { Method = Method.Get, Timeout = 300000 }; request.AddHeader("clientID", ""); ConnectwiseClient.Authenticator = new HttpBasicAuthenticator(""); var response = ConnectwiseClient.Execute(request); var result = JsonConvert.DeserializeObject<List<ContactJson>>(response.Content);
问题现象
接口返回25条数据,反序列化后result包含25个元素,但所有元素均为null,而response.Content存在有效JSON内容。
排查与修复方案
- 根类型不匹配:接口返回的是
Contact对象的数组([]包裹),但反序列化时用了List<ContactJson>,而ContactJson内部包含List<Contact>,结构完全不匹配。应直接反序列化为List<Contact>:var result = JsonConvert.DeserializeObject<List<Contact>>(response.Content); - 属性名称/拼写错误:
- JSON中的
unsubscribeFlag在实体类中写成unsubscrubeFlag(拼写错误),导致无法映射 - JSON中的
defaultBillingFlag在实体类中写成defaultbillingflag(大小写不匹配,默认序列化规则区分大小写) - JSON中的
_info在实体类中写成info,名称不一致,需添加JsonProperty特性修正:[JsonProperty("_info")] public string info { get; set; }
- JSON中的
- 复杂类型错误声明为字符串:JSON中的
site、companyLocation、communicationItems、types、customFields都是对象/数组类型,但实体类中声明为string,无法完成映射。需为这些类型定义对应实体类,例如:
然后将public class Site { public int id { get; set; } public string name { get; set; } [JsonProperty("_info")] public SiteInfo Info { get; set; } } public class SiteInfo { public string site_href { get; set; } }Contact类中的site属性改为public Site site { get; set; },同理处理其他复杂类型。 - Company类缺失属性:JSON中的
company包含identifier字段,实体类中未定义,若需要该字段需补充;同时_info字段也需要添加对应实体类和JsonProperty特性。
内容的提问来源于stack exchange,提问作者ethan
相关产品推荐
相关产品推荐

