[英]Deserializing JSON list/array to C# classes
I am trying to deserialize the following into C# classes: 我正在尝试将以下内容反序列化为C#类:
{
"response" : {
"" : {
"Expense" : [[{
"chart_of_accounts_id" : "45f2fd8f-68b2-44cc-b07ac031c97cd96c",
"account_name" : "Salaries",
"amount" : "1500.00",
"entry_type" : "Debit",
"business_id" : "528f00bb-8cd8-4e7f-be6a-0724c327a7be",
"account_category" : "5"
}, {
"chart_of_accounts_id" : "45f2fd8f-68b2-44cc-b07ac031c97cd96c",
"account_name" : "Salaries",
"amount" : "200.00",
"entry_type" : "Debit",
"business_id" : "528f00bb-8cd8-4e7f-be6a-0724c327a7be",
"account_category" : "5"
}
]]
}
},
"messages" : {
"msgs" : "",
"errs" : ""
}
}
I have the following so far but I get the error "cannot deserialize the current JSON object (eg {"name":"value"}) into type 'Systems.Collections.Generic.List'1[eko_app.Expenses+ExpensesResponse]' because it requires a JSON array (eg [1,2,3]) to deserialize correctly" 到目前为止,我有以下内容,但出现错误“无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'Systems.Collections.Generic.List'1 [eko_app.Expenses + ExpensesResponse]'因为它需要JSON数组(例如[1,2,3])才能正确反序列化”
public class Expense
{
public string chart_of_accounts_id { get; set; }
public string account_name { get; set; }
public decimal amount { get; set; }
public string entry_type { get; set; }
public string business_id { get; set; }
public int account_category { get; set; }
}
public class ExpensesResponse
{
public List<Expense> Expense { get; set; }
}
public class Messages
{
public string msgs { get; set; }
public string errs { get; set; }
}
public class RootObject
{
public List<ExpensesResponse> response { get; set; }
public Messages messages { get; set; }
}
// deserialize the json to c# .net
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);
if (response != null)
{
expenses = response.response;
}
What should I do to correct this? 我该怎么做才能纠正这个问题?
I have deserialized the data with following types. 我用以下类型反序列化了数据。 There was a property with empty name, it should have
JsonPropertyAttribute
. 有一个名称为空的属性,它应该具有
JsonPropertyAttribute
。
public class Expense
{
public string chart_of_accounts_id { get; set; }
public string account_name { get; set; }
public decimal amount { get; set; }
public string entry_type { get; set; }
public string business_id { get; set; }
public int account_category { get; set; }
}
public class ExpensesResponse
{
[JsonProperty(PropertyName = "")]
public ExpensesResponseContent Content { get; set; }
}
public class ExpensesResponseContent
{
public List<List<Expense>> Expense { get; set; }
}
public class Messages
{
public string msgs { get; set; }
public string errs { get; set; }
}
public class RootObject
{
public ExpensesResponse response { get; set; }
public Messages messages { get; set; }
}
You can check how your data is structured with Online JSON Viewer . 您可以使用Online JSON Viewer检查数据的结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.