我正在尝试从这个格式转换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"
}
},
{
more of the same
},
变成这个格式
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,即返回的项目数量,但该对象中的所有项目都为空
结果对象的计数为25,这是返回的项目数量,但该对象中的所有项目都为空。当查看响应对象时,我知道响应不是空的。
假设response.Content
正是您所说的,那么您有以下问题:
- 你的类期待
strings
为classes
的几个字段。快速解决方法是使它们成为对象。例如
public object site { get; set; }
...
public object companyLocation { get; set; }
public object communicationItems { get; set; }
public object types { get; set; }
public object customFields { get; set; }
...
public object info { get; set; }
- 反序列化到错误的对象。应该是
JsonConvert.DeserializeObject<List<Contact>>
而不是JsonConvert.DeserializeObject<List<ContactJson>>
- 你在
companyLocation.location_href
下缺少"
,但这可能只是在你的SO问题中,因为如果它缺少代码,它会抛出而不是给你null
s