繁体   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