1

I am trying to Deserialize a json string into a class, however, the property keeps coming up null.

IRestResponse companyResponse = companyClient.Execute(companyRequest);
Company companyList = JsonConvert.DeserializeObject<Company>(companyResponse.Content);

public class Company
{
     public string id { get; set; }
}

The company get request pulls in several other pieces of data, but I am only interested in the ID value. From what I have read on this site as well as on the internet, the above should work.

EDIT: The returning get

"{\"companies\":[{\"id\":\"7f91f557-4d12-486e-ba89-09c46c8a56b7\",\"name\":\"storEDGE Demo\",\"contact_email\":\"\",\"contact_phone\":\"5555555555\",\"rental_center_subdomain\":null,\"websites\":[{\"provider\":\"storedge\",\"provider_id\":\"storedgedemo\"}],\"eligible_for_voyager_website\":[{\"provider_exists\":true,\"api_association_exists\":true}],\"sales_demo\":true,\"pusher_channel\":\"private-company-7f91f5574d12486eba8909c46c8a56b7\",\"address\":{\"id\":\"b4afccb5-4d5d-4ccd-9965-67280cb868c5\",\"address1\":\"1234 storEDGE St. \",\"address2\":\"\",\"city\":\"Kansas City\",\"state\":\"MO\",\"postal\":\"66205\",\"country\":\"US\",\"full_address\":\"1234 storEDGE St. , Kansas City, MO 66205\",\"latitude\":null,\"longitude\":null,\"time_zone_id\":\"America/Chicago\",\"time_zone_offset\":\"-05:00\",\"invalid_data\":false,\"label\":\"Home\"}},{\"id\":\"249f62c7-64fb-4e12-ac91-9a4a30c1ab1c\",\"name\":\"SafeStor Insurance Company\",\"contact_email\":\"[email protected]\",\"contact_phone\":\"\",\"rental_center_subdomain\":null,\"websites\":[],\"eligible_for_voyager_website\":[{\"provider_exists\":false,\"api_association_exists\":false}],\"sales_demo\":false,\"pusher_channel\":\"private-company-249f62c764fb4e12ac919a4a30c1ab1c\",\"address\":{\"id\":\"aa5a7775-1eb9-4fc6-8b91-b2179b9fd1fb\",\"address1\":\"5901 Catalina\",\"address2\":\"\",\"city\":\"Fairway\",\"state\":\"KS\",\"postal\":\"66205\",\"country\":\"US\",\"full_address\":\"5901 Catalina, Fairway, KS 66205\",\"latitude\":null,\"longitude\":null,\"time_zone_id\":\"America/Chicago\",\"time_zone_offset\":\"-05:00\",\"invalid_data\":false,\"label\":\"Home\"}}],\"meta\":{\"pagination\":{\"current_page\":1,\"total_pages\":1,\"per_page\":100,\"total_entries\":2,\"previous_page\":null,\"next_page\":null},
2
  • please post the json contents Commented Apr 20, 2018 at 16:42
  • @RicardoPontual posted! sorry It's a mess to read though. Commented Apr 20, 2018 at 16:45

2 Answers 2

1

You are deseiralizing the JSON to the wrong type.

The JSON shown is a JSON object with an array property, which I would assume is the collection of companies.

Use the following model

public class RootObject {
    public Company[] companies { get; set; }
}

From there you can

var json = companyResponse.Content;
var result = JsonConvert.DeserializeObject<RootObject>(json);
Company[] companies = result.companies;
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, in the provided get, that's for a demo company, but in production, there were be tens if not hundreds of companies that will be provided. Trying out your method here and seeing how it goes! I appreciate your response.
It works! question, do I need to have it as a rootobject name? or Can i specify RootObjectCompany to it? I have several other objects I will need to do this for and will need to specify which is what.
@ggiaquin16 You can name it what ever you like This was just an example.
Awesome, I was wondering because the built in json to class converter had previously created somethig named rootobject before (without really understanding why I deleted it), I will need to go back and add it but making sure it wasn't something that needed that name specifically or not.
@ggiaquin16 the name is used in examples more to get the point across that you are dealing with nested objects with this one being the top most one. A lot of tools follow the same naming pattern
1

Your Deserialization is expecting json content of object with property id which is clearly not the content that is passing.

By the file content you posted, you should add another class and change deserialization type.

public class Response
{
    [JsonProperty("companies")
    public Company[] Companies { get; set; }
}

var companiesResponse = JsonConvert.DeserializeObject<Response>(companyResponse.Content);

2 Comments

Orel, thank you so much for your response, Trying this out as well as the other provided answer! I appreciate it.
This was fairly easy to implement, short and sweet and it worked! Thank you a ton for the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.