简体   繁体   中英

Json Deserialize into object C# Json.net

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\":\"ponderosainsurance@gmail.com\",\"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},

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);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM