[英]Deserializing JSON list/array to 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" : ""
}
}
到目前為止,我有以下內容,但出現錯誤“無法反序列化當前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;
}
我該怎么做才能糾正這個問題?
我用以下類型反序列化了數據。 有一個名稱為空的屬性,它應該具有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; }
}
您可以使用Online JSON Viewer檢查數據的結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.