C# 无法使用列表中的列表反序列化JSON

C# 无法使用列表中的列表反序列化JSON,c#,.net,json,json-deserialization,C#,.net,Json,Json Deserialization,我有一个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",

我有一个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);
尝试使用
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","",""]
   }
 }
现在让我们看看你们的课程
jsonResult.Result.ReportMasterDetailsData.Data
将返回null,因为在JSON响应属性中调用的
Result
不是
ReportMasterDetailsData
,所以将
ReportMasterDetailsData
属性重命名为
Result
。如果不想更改属性的名称,可以在属性中添加
[JsonProperty(“Result”)]

ReportMasterDetailsData
类的
Data
属性类型为
List
,但在JSON响应中,此集合没有属性名称。您可以将
List
更改为
List
,然后以某种方式将其转换为
ReportMasterDetails
类。也可以不使用属性名对其进行反序列化。看和看。我希望这能帮助您

我建议使用json字符串创建模型,并使用
jsonvert.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]}");
    }
公共类结果
{
公共列表数据{get;set;}
公共列表头{get;set;}
公共列表页脚{get;set;}
}
公共类ReportRootObject
{
公共字符串S{get;set;}
公共结果结果{get;set;}
}
静态void Main(字符串[]参数)
{
以下几方面的数据::[225512570205,“05-9-9-9月19日09:53上午上午9点,”,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“Rs.0\”,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[22456410400、\'01-Sep-19 08:46 AM\'、\'16\'、\'Rs.726.00\'、\'Card\'、\'Amount:726,Type:Visa\']、\'Header\':[\'S.No.\'、\'Order Id\'、\'Date\'、\'Table No\'、'Amount\'、\'S\'、'S付款方式\'、'More Info\'、'更多信息\]、'Footer\':[\'Total\'、'4\'、'S\'、'S\'、'1386、'S\'、';
ReportRootObject jsonResult=新的ReportRootObject();
PopulateObject(响应,jsonResult);
Console.WriteLine($“SerialNumber:{jsonResult.Result.Data[0][0]}”);
}

试试这个
JsonConvert.DeserializeObject
@Nkosi请详细说明?使用``json2csharp.com``我得到
公共类结果{公共列表数据{get;set;}公共列表头{get;set;}公共列表尾{get;set;}公共类根对象{public string S{get;set;}公共结果{get;set;}}
@mtkachenko这不是问题所在。
GenericJSONResponse
只是一个泛型对象,它不应该是:jsonResult.ReportRootObject.ReportMasterDetailsData.Data;或者一些cast…@Nkosi不确定你的意思。。这并不能回答问题。。为什么必须调用Result属性?你在qu中检查了我的代码吗estion???@PranjalVatsa好的,如果您不想更改名称,可以在PropertyTanks@krishna mohan varma中添加[JsonProperty(“结果”)]
 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; }
    }
{
  "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","",""]
   }
 }
    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]}");
    }