I am trying to convert JSON from this format
[
{
"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
},
Into this format
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; }
}
I am parsing it like this
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);
The result object then has a count of 25 which is the amount of items returned but all of the items in this object are null
The result object then has a count of 25 which is the amount of items returned but all of the items in this object are null. When looking at the response object I know that the response isn't null.
Assuming response.Content
is exactly what you say it is, then you have the following problems:
strings
for a few fields that are classes
. Quickfix is to make them objects. For examplepublic 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>>
instead of
JsonConvert.DeserializeObject<List<ContactJson>>
"
under companyLocation.location_href
but that might be just in your SO question because if it was missing the code it would just throw istead of giving you null
s