簡體   English   中英

API Json反序列化C#類

[英]API Json Deserialize C# Class

我正在嘗試反序列化來自API的Json響應。 數據看起來像這樣。

 {
      "api": {
        "results": 1,
        "aaa": [
          [
            {
              "bbb": 1,
              "ccc": 85,
              "ddd": "this is  test1",
              "eee": "this is  test2",
              "fff": "orang",
              "ggg": "apple",
              "hhh": "this is test3",
              "iii": {
                "iia": 35,
                "iib": 27,
                "iic": 4
              },
              "jjj": {
                "jja": 18,
                "jjb": 16,
                "jjc": 2
              },
              "kkk": {
                "kka": 17,
                "kkb": 11,
                "kkc": 2,

              },
              "lll": 67,
              "mmm": 85,
              "nnn": "2019-05-04"
            }
          ]
        ]
      }
    }

並且,有關此案例的有關Json模式的更多信息是

 {
      "type": "object",
      "properties": {
        "api": {
          "type": "object",
          "properties": {
            "results": {
              "type": "integer"
            },
            "aaa": {
              "type": "array",
              "items": {
                "type": "array",
                "items": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }

並且我創建了一個自定義類來讀取json內容http://json2csharp.com/

public class Api
{
    public int results { get; set; }
    public List<List<>> aaa { get; set; }
}

public class RootObject
{
    public Api api { get; set; }
}

這個json的正確類是什么? 我該怎么做才能糾正這個問題?

您必須為每個對象創建一個類。 因此,您必須為此部分創建一個類:

"items": {
            "type": "object"
         }

然后在此行中使用此類:

public List<List<Item>> aaa { get; set; }

根據您提供的模式,這應該起作用:

public class Api
{
    public Dictionary<string, object>[][] Aaa { get; set; }
    public long? Results { get; set; }
}

但是,如果您想完全反序列化該類,則可以使用如下代碼:

public class FinalClass
{
    public Api Api { get; set; }
}

public class Api
{
    public long Results { get; set; }

    public Aaa[][] Aaa { get; set; }
}

public class Aaa
{
    public long Bbb { get; set; }

    public long Ccc { get; set; }

    public string Ddd { get; set; }

    public string Eee { get; set; }

    public string Fff { get; set; }

    public string Ggg { get; set; }

    public string Hhh { get; set; }

    public Dictionary<string, long> Iii { get; set; }

    public Dictionary<string, long> Jjj { get; set; }

    public Dictionary<string, long> Kkk { get; set; }

    public long Lll { get; set; }

    public long Mmm { get; set; }

    public DateTimeOffset Nnn { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM