[英]Deserialize JSON array into c# list
I am trying to deserialize a JSON array into a list of objects.我正在尝试将 JSON 数组反序列化为对象列表。 I know how to normally do this but the JSON is returning in a format I don't know how to with.
我知道通常如何执行此操作,但 JSON 以我不知道如何使用的格式返回。
{
"result": {
"account": [
{
"id": "18192504834",
"ownerID": null,
"accountName": "Citrix",
"industry": null,
"phone": "",
"annualRevenue": null,
"numberOfEmployees": "0",
"website": "",
"yearStarted": null,
"fax": null,
"billingCity": null,
"billingCountry": null,
"billingPostalCode": null,
"billingState": null,
"billingStreetAddress": null,
"shippingCity": null,
"shippingCountry": null,
"shippingPostalCode": null,
"shippingState": null,
"shippingStreetAddress": null,
"account_status_634798fb3a786": null,
"company_linkedin_url_6347990655c57": null,
"focus_area___primary_634799ab2f32f": null,
"normalised___industry_634799cabc901": null,
"normalised___number_of_employees_63479a161a8e2": null,
"target_markets_63479a4007cd9": null,
"focus_area___secondary_63479a64b0d21": null,
"normalised___address_1_63479aa361d84": null,
"normalised___address_2_63479ac2a3ede": null,
"normalised___town__city_63479acf156f5": null,
"normalised___state_63479b13aa948": null,
"normalised___post_code_63479b66a1b77": null,
"normalised___country_63479c84d2e4d": null,
"normalised___region_63479cb3129f9": null,
"bulk_import_list_source_63479cd899e77": null,
"cwid_635887724a213": null
},
Here is the class I am trying to convert it into这是 class 我正在尝试将其转换为
public class Account{
public string id { get; set; }
public string ownerID { get; set; }
public string AccountName { get; set; }
public string Industry { get; set; }
public string Phone { get; set; }
public string AnnualRevenue { get; set; }
public string NumberOfEmployees { get; set; }
public string Website { get; set; }
public string YearStarted { get; set; }
public string Fax { get; set; }
public string BillingCity { get; set; }
public string BillingCountry { get; set; }
public string BillingPostalCode { get; set; }
public string BillingState { get; set; }
public string BillingStreetAddress { get; set; }
public string ShippingCity { get; set; }
public string ShippingCountry { get; set; }
public string ShippingPostalCode { get; set; }
public string ShippingState { get; set; }
public string ShippingStreetAddress { get; set; }
public string AccountStatus { get; set; }
public string CompanyLinkedinURL { get; set; }
public string FocusArea { get; set; }
public string NormalisedIndustry { get; set; }
public string NormalisedNumberOfEmployees { get; set; }
public string TargetMarkets { get; set; }
public string FocusAreaSecondary { get; set; }
public string NormalisedAddress { get; set; }
public string NormalisedAddressTwo { get; set; }
public string NormalisedTownCity { get; set; }
public string NormalisedState { get; set; }
public string NormalisedPostCode { get; set; }
public string NormalisedCountry { get; set; }
public string NormalisedRegion { get; set; }
public string BulkImportList { get; set; }
public string CWID { get; set; }
}
Here is how I am retrieving the JSON and attempting to deserialize it这是我检索 JSON 并尝试反序列化它的方式
var request2 = new RestRequest()
{
Method = Method.Post,
Timeout = 300000
};
var SharpSpringClient = new RestClient("https://api.sharpspring.com/pubapi/v1.2/?accountID=&secretKey=");
var json = "{\"method\":\"getAccounts\",\"id\":\"zapier_getaccount\",\"params\":{\"where\":{}}}";
request2.AddStringBody(json, ContentType.Json);
RestResponse response2 = SharpSpringClient.Execute(request2);
var result2 = JsonConvert.DeserializeObject<List<Account>>(response2.Content);
This then fails with Cannot deserialize the current JSON object..... I know it's failing because a list of Accounts doesn't account for the result and account json objects.然后失败并显示 Cannot deserialize the current JSON object..... 我知道它失败了,因为帐户列表不考虑结果和帐户 json 对象。
I also tried the answer from this question Not able to deserialize JSON array into C# list and made these classes我也尝试了这个问题的答案Not able to deserialize JSON array into C# list并制作了这些类
public class RootObject
{
[JsonProperty("result")]
public string result { get; set; }
public RootAccount rootAccount { get; set; }
}
public class RootAccount
{
[JsonProperty("account")]
public string account { get; set; }
public List<Account> accounts { get; set; }
}
Your class structure should look something like this你的 class 结构应该是这个样子
public class Result
{
public List<Account> account { get; set; }
}
public class RootObject
{
public Result result { get; set; }
}
For deserializing:对于反序列化:
var result2 = JsonConvert.DeserializeObject<RootObject>(response2.Content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.