【问题标题】:Deserialize complex nested JSON with C#使用 C# 反序列化复杂的嵌套 JSON
【发布时间】:2021-12-20 11:19:19
【问题描述】:

我想在 C# 中读取类似这样的 JSON。但我不知道如何从 JSON 中提取特定的块。

{
  "took": 32,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 263,
      "relation": "eq"
    },
    "max_score": 29.078617,
    "hits": [
      {
        "_index": "x",
        "_type": "_doc",
        "_id": "x",
        "_score": 29.078617,
        "_source": {
          "reference": "x",
          "locationList": [
            {
              "country": "x",
              "address": "x",
              "city": "x",
              "postalCode": "x",
              "latitude": "x",
              "county": "x",
              "municipal": "x",
              "longitude": "x"
            }
          ],
          "expires": "x",
          "businessName": "x",
          "employer": { "name": "x" },
          "source": "x",
          "published": "x",
          "title": "x",
          "uuid": "x",
          "properties": {
            "applicationdue": "x",
            "employer": "x"
          },
          "status": "ACTIVE"
        }
      },
      ....
  • 如何解析第二个“点击”中的数据?

还没有找到任何关于如何在 JSON 中使用多个框的好读物,所以希望有经验的人会知道这个。谢谢。

【问题讨论】:

  • “第二个hits”(它是一个数组)是“第一个hits”的一个属性。 “第一个hits”是您的顶级对象的属性。为了反序列化,构建一组与 JSON 结构匹配的类,并将所有内容反序列化为顶级类的实例
  • 你可以从使用正确的词开始——你想解析(即你正在写一个解析器)还是你想反序列化(即您正在使用已经存在的库,例如 Newtonsoft)。

标签: c# json


【解决方案1】:

你可以试试这样的

 var jsonResponse = JObject.Parse(yourJason);
 var jHits = (JArray)jsonResponse["hits"]["hits"]; //This way you'll get the second hits array.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多