簡體   English   中英

Json反序列化而無需聲明類

[英]Json deserialization without declaration of a class

我有一個json格式的http響應,我需要使用JSON.Net庫進行反序列化。 我使用http://json2csharp.com/創建了我需要的類,但是有沒有一種方法可以在不聲明所有類的情況下進行呢? 我只需要3-4個聲明的字段中的3-4個。

響應:

{
  "query": {
    "ids": [42354854],
    "dimensions": ["ym:s:gender"],
    "metrics": ["ym:s:visits", "ym:s:users", "ym:s:avgVisitDurationSeconds"],
    "sort": ["-ym:s:visits"],
    "date1": "2017-03-01",
    "date2": "2017-05-09",
    "group": "Week",
    "auto_group_size": "1",
    "quantile": "50",
    "attribution": "Last",
    "currency": "RUB",
    "auto_group_type": "week"
  },
  "data": [{
    "dimensions": [{
      "name": "мужской",
      "id": "male"
    }],
    "metrics": [
      [19.0, 42.0, 58.0, 24.0, 13.0, 42.0, 54.0, 20.0, 5.0, 10.0, 3.0],
      [11.0, 17.0, 15.0, 12.0, 5.0, 13.0, 15.0, 4.0, 4.0, 5.0, 2.0],
      [227.26315789, 275.85714286, 217.29310345, 312.54166667, 42.07692308, 119.38095238, 120.12962963, 136.85, 156.6, 142.6, 94.66666667]
    ]
  }, {
    "dimensions": [{
      "name": "женский",
      "id": "female"
    }],
    "metrics": [
      [6.0, 18.0, 19.0, 3.0, 0.0, 2.0, 4.0, 0.0, 1.0, 0.0, 0.0],
      [2.0, 4.0, 5.0, 3.0, 0.0, 1.0, 2.0, 0.0, 1.0, 0.0, 0.0],
      [1073.0, 163.66666667, 158.42105263, 20.0, 0.0, 23.5, 12.75, 0.0, 21.0, 0.0, 0.0]
    ]
  }],
  "total_rows": 11,
  "total_rows_rounded": false,
  "sampled": false,
  "sample_share": 1.0,
  "sample_size": 414,
  "sample_space": 414,
  "data_lag": 81,
  "totals": [
    [25.0, 60.0, 77.0, 27.0, 13.0, 44.0, 58.0, 20.0, 6.0, 10.0, 3.0],
    [13.0, 21.0, 20.0, 15.0, 5.0, 14.0, 17.0, 4.0, 5.0, 5.0, 2.0],
    [430.24, 242.2, 202.76623377, 280.03703704, 42.07692308, 115.02272727, 112.72413793, 136.85, 134.0, 142.6, 94.66666667]
  ],
  "time_intervals": [
    ["2017-03-01", "2017-03-05"],
    ["2017-03-06", "2017-03-12"],
    ["2017-03-13", "2017-03-19"],
    ["2017-03-20", "2017-03-26"],
    ["2017-03-27", "2017-04-02"],
    ["2017-04-03", "2017-04-09"],
    ["2017-04-10", "2017-04-16"],
    ["2017-04-17", "2017-04-23"],
    ["2017-04-24", "2017-04-30"],
    ["2017-05-01", "2017-05-07"],
    ["2017-05-08", "2017-05-09"]
  ]
}

以及網站上的課程:

    public class Query
{
    public List<int> ids { get; set; }
    public List<string> dimensions { get; set; }
    public List<string> metrics { get; set; }
    public List<string> sort { get; set; }
    public string date1 { get; set; }
    public string date2 { get; set; }
    public string group { get; set; }
    public string auto_group_size { get; set; }
    public string quantile { get; set; }
    public string attribution { get; set; }
    public string currency { get; set; }
    public string auto_group_type { get; set; }
}

public class Dimension
{
    public string name { get; set; }
    public string id { get; set; }
}

public class Datum
{
    public List<Dimension> dimensions { get; set; }
    public List<List<double>> metrics { get; set; }
}

public class RootObject
{
    public Query query { get; set; }
    public List<Datum> data { get; set; }
    public int total_rows { get; set; }
    public bool total_rows_rounded { get; set; }
    public bool sampled { get; set; }
    public double sample_share { get; set; }
    public int sample_size { get; set; }
    public int sample_space { get; set; }
    public int data_lag { get; set; }
    public List<List<double>> totals { get; set; }
    public List<List<string>> time_intervals { get; set; }
}

創建一個僅包含您要反序列化的字段的類,Json.NET足夠聰明,可以將您的Json對象反序列化為這些字段:

示例Json:

{
    "foo": "bar",
    "foo1": {
        "foo2": 2,
        "foo3": "bar1",
    },
    "foo4": 12
}

可以反序列化為:

public class FooContainer
{
    public string Foo { get; set; }
    public int Foo4 { get; set; }
}

這樣, foo1屬性將被忽略。

我想我和你有同樣的問題。 這可能會有所幫助。

使用JSON.NET反序列化任何內容

它的網絡是您可以反序列化為JToken,然后根據JToken的類型進行處理。

另一種方法是反序列化為JObject,或反序列化為Dictionary。 對於嵌入式字典和列表,KeyValuePair的值將是一個JObject,然后可以對其進行解析/處理。

暫無
暫無

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

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