【问题标题】:Unable to deserialise JSON with list inside a list无法使用列表中的列表反序列化 JSON
【发布时间】:2019-09-05 07:58:05
【问题描述】:

我有一个带有来自 API 的数据的 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","",""]}}

我需要将其转换为 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; }
    }

当尝试使用 jsonResult.Result.ReportMasterDetailsData.Data; 访问字段时 我在“ReportMasterDetailsData”处收到空引用异常

我错过了什么??

【问题讨论】:

  • 试试这个JsonConvert.DeserializeObject&lt;ReportRootObject&gt;
  • @Nkosi 请详细说明?使用```json2csharp.com```我得到public class Result { public List&lt;List&lt;object&gt;&gt; Data { get; set; } public List&lt;string&gt; Headers { get; set; } public List&lt;string&gt; Footer { get; set; } } public class RootObject { public string S { get; set; } public Result Result { get; set; } }
  • @mtkachenko 这不是问题。GenericJSONResponse 只是一个通用对象
  • 不应该是这样的:jsonResult.ReportRootObject.ReportMasterDetailsData.Data;或一些演员......
  • @Nkosi 不确定你的意思..

标签: c# .net json json-deserialization


【解决方案1】:

好吧,让我们以格式化的 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&lt;List&lt;ReportMasterDetails&gt;&gt;,但在 JSON 响应中,此集合没有属性名称。您可以将List&lt;List&lt;ReportMasterDetails&gt;&gt; 更改为List&lt;List&lt;string&gt;&gt;,然后以某种方式将其转换为ReportMasterDetails 类。或者您可以在不使用属性名称的情况下对其进行反序列化。查看How to deserialize a child object with dynamic (numeric) key names?C# serialize JSON without property name。希望对你有帮助

【讨论】:

  • 这不能回答问题..为什么必须将属性称为结果?你有没有检查我的代码??
  • @PranjalVatsa 好的,如果您不想更改名称,可以在您的属性中添加 [JsonProperty("Result")]
【解决方案2】:

我建议使用 json2csharp 从 json 字符串创建模型,然后使用 JsonConvert.PopulateObject 完成剩下的工作。这是我试过的代码sn-p。

    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]}");
    }

【讨论】:

  • 谢谢@krishna-mohan-varma
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2016-05-30
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
相关资源
最近更新 更多