无法使用列表中的列表反序列化 JSON
Unable to deserialise JSON with list inside a list
我有一个 JSON,数据来自 API
{"S":"Success","Result":{"Data":[[2251,2570205,"05-Sep-19 09:53 AM","--","Rs. 0","Cash","Amount Paid : 0"],[2248,3817456,"01-Sep-19 08:53 AM","--","Rs. 168.00","NC","Reason : NC"],[2247,2997168,"01-Sep-19 08:49 AM","16","Rs. 660.00","Card","Amount Paid : 660, Type : Visa"],[2245,6410400,"01-Sep-19 08:46 AM","16","Rs. 726.00","Card","Amount Paid : 726, Type : Visa"]],"Headers":["S.No.","Order Id","Date","Table No","Amount","Mode of Payment","More Info"],"Footer":["Total","4","","","Rs. 1,386.00","",""]}}
我需要将其转换为 C# 对象才能绑定到 UI。下面是 C# 代码
反序列化对象的代码:
var jsonResult = JsonConvert.DeserializeObject<GenericJSONResponse<ReportRootObject>>(response);
我在 response
对象中得到响应。
对象:
public class ReportRootObject
{
public string S { get; set; }
public ReportMasterDetailsData ReportMasterDetailsData { get; set; }
}
public class ReportMasterDetailsData
{
public List<List<ReportMasterDetails>> Data { get; set; }
public List<string> Headers { get; set; }
public List<string> Footer { get; set; }
}
public class ReportMasterDetails
{
public int SerialNumber { get; set; }
public int OrderId { get; set; }
public string Date { get; set; }
public string TableNumber { get; set; }
public string Amount { get; set; }
public string ModeOfPayment { get; set; }
public string Reason { get; set; }
}
WHen 尝试使用 jsonResult.Result.ReportMasterDetailsData.Data;
访问字段
我在 'ReportMasterDetailsData'
处收到空引用异常
我错过了什么??
好吧,让我们看看您的 JSON 结果,其格式为 json:
{
"S":"Success",
"Result":
{
"Data":
[
[2251,2570205,"05-Sep-19 09:53 AM","--","Rs. 0","Cash","Amount Paid : 0"],
[2248,3817456,"01-Sep-19 08:53 AM","--","Rs. 168.00","NC","Reason : NC"],
[2247,2997168,"01-Sep-19 08:49 AM","16","Rs. 660.00","Card","Amount Paid : 660, Type : Visa"],
[2245,6410400,"01-Sep-19 08:46 AM","16","Rs. 726.00","Card","Amount Paid : 726, Type : Visa"]
],
"Headers":["S.No.","Order Id","Date","Table No","Amount","Mode of Payment","More Info"],
"Footer":["Total","4","","","Rs. 1,386.00","",""]
}
}
现在让我们看看您的 classes。 jsonResult.Result.ReportMasterDetailsData.Data
将 return 为空,因为在您的 JSON 响应中 属性 调用了 Result
而不是 ReportMasterDetailsData
,因此重命名 ReportMasterDetailsData
属性 到 Result
。如果您不想更改 属性 的名称,您可以在 属性
中添加 [JsonProperty("Result")]
Data
属性 的 ReportMasterDetailsData
class 的类型是 List<List<ReportMasterDetails>>
,但在 JSON 响应中,此集合没有 属性 名字。您可以将 List<List<ReportMasterDetails>>
更改为 List<List<string>>
,然后以某种方式将其转换为 ReportMasterDetails
class。或者您可以不使用 属性 名称反序列化它。看看 and 。希望对您有所帮助
我建议使用 json2csharp 从 json 字符串创建模型并使用 JsonConvert.PopulateObject
完成其余工作。这是我试过的代码片段。
public class Result
{
public List<List<object>> Data { get; set; }
public List<string> Headers { get; set; }
public List<string> Footer { get; set; }
}
public class ReportRootObject
{
public string S { get; set; }
public Result Result { get; set; }
}
static void Main(string[] args)
{
string response = "{\"S\":\"Success\",\"Result\":{\"Data\":[[2251,2570205,\"05 - Sep - 19 09:53 AM\",\"--\",\"Rs. 0\",\"Cash\",\"Amount Paid : 0\"],[2248,3817456,\"01 - Sep - 19 08:53 AM\",\"--\",\"Rs. 168.00\",\"NC\",\"Reason: NC\"],[2247,2997168,\"01 - Sep - 19 08:49 AM\",\"16\",\"Rs. 660.00\",\"Card\",\"Amount Paid : 660, Type: Visa\"],[2245,6410400,\"01 - Sep - 19 08:46 AM\",\"16\",\"Rs. 726.00\",\"Card\",\"Amount Paid : 726, Type: Visa\"]],\"Headers\":[\"S.No.\",\"Order Id\",\"Date\",\"Table No\",\"Amount\",\"Mode of Payment\",\"More Info\"],\"Footer\":[\"Total\",\"4\",\"\",\"\",\"Rs. 1,386.00\",\"\",\"\"]}}";
ReportRootObject jsonResult = new ReportRootObject();
JsonConvert.PopulateObject(response, jsonResult);
Console.WriteLine($"SerialNumber: {jsonResult.Result.Data[0][0]}");
}
我有一个 JSON,数据来自 API
{"S":"Success","Result":{"Data":[[2251,2570205,"05-Sep-19 09:53 AM","--","Rs. 0","Cash","Amount Paid : 0"],[2248,3817456,"01-Sep-19 08:53 AM","--","Rs. 168.00","NC","Reason : NC"],[2247,2997168,"01-Sep-19 08:49 AM","16","Rs. 660.00","Card","Amount Paid : 660, Type : Visa"],[2245,6410400,"01-Sep-19 08:46 AM","16","Rs. 726.00","Card","Amount Paid : 726, Type : Visa"]],"Headers":["S.No.","Order Id","Date","Table No","Amount","Mode of Payment","More Info"],"Footer":["Total","4","","","Rs. 1,386.00","",""]}}
我需要将其转换为 C# 对象才能绑定到 UI。下面是 C# 代码
反序列化对象的代码:
var jsonResult = JsonConvert.DeserializeObject<GenericJSONResponse<ReportRootObject>>(response);
我在 response
对象中得到响应。
对象:
public class ReportRootObject
{
public string S { get; set; }
public ReportMasterDetailsData ReportMasterDetailsData { get; set; }
}
public class ReportMasterDetailsData
{
public List<List<ReportMasterDetails>> Data { get; set; }
public List<string> Headers { get; set; }
public List<string> Footer { get; set; }
}
public class ReportMasterDetails
{
public int SerialNumber { get; set; }
public int OrderId { get; set; }
public string Date { get; set; }
public string TableNumber { get; set; }
public string Amount { get; set; }
public string ModeOfPayment { get; set; }
public string Reason { get; set; }
}
WHen 尝试使用 jsonResult.Result.ReportMasterDetailsData.Data;
访问字段
我在 'ReportMasterDetailsData'
我错过了什么??
好吧,让我们看看您的 JSON 结果,其格式为 json:
{
"S":"Success",
"Result":
{
"Data":
[
[2251,2570205,"05-Sep-19 09:53 AM","--","Rs. 0","Cash","Amount Paid : 0"],
[2248,3817456,"01-Sep-19 08:53 AM","--","Rs. 168.00","NC","Reason : NC"],
[2247,2997168,"01-Sep-19 08:49 AM","16","Rs. 660.00","Card","Amount Paid : 660, Type : Visa"],
[2245,6410400,"01-Sep-19 08:46 AM","16","Rs. 726.00","Card","Amount Paid : 726, Type : Visa"]
],
"Headers":["S.No.","Order Id","Date","Table No","Amount","Mode of Payment","More Info"],
"Footer":["Total","4","","","Rs. 1,386.00","",""]
}
}
现在让我们看看您的 classes。 jsonResult.Result.ReportMasterDetailsData.Data
将 return 为空,因为在您的 JSON 响应中 属性 调用了 Result
而不是 ReportMasterDetailsData
,因此重命名 ReportMasterDetailsData
属性 到 Result
。如果您不想更改 属性 的名称,您可以在 属性
[JsonProperty("Result")]
Data
属性 的 ReportMasterDetailsData
class 的类型是 List<List<ReportMasterDetails>>
,但在 JSON 响应中,此集合没有 属性 名字。您可以将 List<List<ReportMasterDetails>>
更改为 List<List<string>>
,然后以某种方式将其转换为 ReportMasterDetails
class。或者您可以不使用 属性 名称反序列化它。看看
我建议使用 json2csharp 从 json 字符串创建模型并使用 JsonConvert.PopulateObject
完成其余工作。这是我试过的代码片段。
public class Result
{
public List<List<object>> Data { get; set; }
public List<string> Headers { get; set; }
public List<string> Footer { get; set; }
}
public class ReportRootObject
{
public string S { get; set; }
public Result Result { get; set; }
}
static void Main(string[] args)
{
string response = "{\"S\":\"Success\",\"Result\":{\"Data\":[[2251,2570205,\"05 - Sep - 19 09:53 AM\",\"--\",\"Rs. 0\",\"Cash\",\"Amount Paid : 0\"],[2248,3817456,\"01 - Sep - 19 08:53 AM\",\"--\",\"Rs. 168.00\",\"NC\",\"Reason: NC\"],[2247,2997168,\"01 - Sep - 19 08:49 AM\",\"16\",\"Rs. 660.00\",\"Card\",\"Amount Paid : 660, Type: Visa\"],[2245,6410400,\"01 - Sep - 19 08:46 AM\",\"16\",\"Rs. 726.00\",\"Card\",\"Amount Paid : 726, Type: Visa\"]],\"Headers\":[\"S.No.\",\"Order Id\",\"Date\",\"Table No\",\"Amount\",\"Mode of Payment\",\"More Info\"],\"Footer\":[\"Total\",\"4\",\"\",\"\",\"Rs. 1,386.00\",\"\",\"\"]}}";
ReportRootObject jsonResult = new ReportRootObject();
JsonConvert.PopulateObject(response, jsonResult);
Console.WriteLine($"SerialNumber: {jsonResult.Result.Data[0][0]}");
}