使用JsonSlurper进行Groovy json解析

yb3bgrhw  于 2022-11-01  发布在  其他
关注(0)|答案(2)|浏览(248)

我尝试使用jsonslurper解析从groovy的elastic-search api中获取的以下json。我需要从该json中创建一个_id列表。尝试了多种代码变体,但没有成功
请建议,任何帮助都感激不尽。

{
  "took" : 2535,
  "timed_out" : false,
  "_shards" : {
    "total" : 384,
    "successful" : 384,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "X",
        "_type" : "_doc",
        "_id" : "310165903526204",
        "_score" : null,
        "sort" : [
          "310165903526204"
        ]
      },
      {
        "_index" : "X",
        "_type" : "_doc",
        "_id" : "310165903698515",
        "_score" : null,
        "sort" : [
          "310165903698515"
        ]
      },
      {
        "_index" : "X",
        "_type" : "_doc",
      **"_id" : "310165903819494"**,
        "_score" : null,
        "sort" : [
          "310165903819494"
        ]
      }
    ]
  }
}

PS:我尝试使用elasticsearch提供的多个客户端来搜索ES和解析数据,但我面临着另一个问题,所以不得不切换到HTTP客户端并手动解析。这是客户端问题RestHighLevelClient with Elasticsearch client error的链接
更新:

{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 370,
    "successful" : 370,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "3961655114649",
        "_score" : 1.0,
        "_source" : {
          "location" : {
            "lat" : 14.94046,
            "lon" : -23.48016
          },
          "place" : {
            "country" : "USA",
            "pois" : [
              {
                "externalIdentifier" : "3961655114649",
                "gdfFeatureCode" : "7376",
                "officialNames" : [
                  {
                    "name" : "ENG",
                    "language" : "ENG"
                  }
                ],
                "alternateNames" : [ ],
                "brandNames" : [ ],
                "streetsAndCities" : [
                  {
                    "city" : "California",
                    "cityLanguage" : "UND"
                  }
                ],
                "postalCode" : "",
                "postalCodeMain" : ""
              }
            ],
            "providers" : [
              {
                "UniqueId" : """{"mostSigBits": 6332787932357083, "leastSigBits": -6052983698683356}""",
                "code" : "ABC",
                "deliveryId" : "3959",
                "rawId" : """{"mostSigBits": 8772023489060096, "leastSigBits": -6327158443391381}""",
                "totalAttributes" : "1",
                "visibleAttributes" : "1"
              },
              {
                "UniqueId" : """{"mostSigBits": 6332787932357083, "leastSigBits": -6052983698683356}_1""",
                "rawId" : """{"mostSigBits": 8772023489060096, "leastSigBits": -6327158443391381}""",
                "totalAttributes" : "1",
                "visibleAttributes" : "1"
              }
            ],
            "attributes" : [ ],
            "isAddObservation" : false
          },
          "transactionCommitDate" : 0
        }
      }
    ]
  }
}

有了这个更新的Json,我想在providers下的typeId中提取mostSigBits和leastSigBits值。但问题是我只想在providers[]中提取那个typeID,它没有_1或_2或任何后缀。
我已经尝试通过这样做来获得数据,但正在寻找一些更好的方法

json.点击数.点击数[0]._源.位置.提供程序[0].类型Id

6ljaweal

6ljaweal1#

def _id = json.hits.hits.collect{ it._id }
_id.each{println it}
qij5mzcb

qij5mzcb2#

基本Groovy的对象导航可以提供帮助:

import groovy.json.*

String response = '''\
{
  "took" : 2535,
  "timed_out" : false,
  "_shards" : {
    "total" : 384,
    "successful" : 384,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "X",
        "_type" : "_doc",
        "_id" : "310165903526204",
        "_score" : null,
        "sort" : [
          "310165903526204"
        ]
      },
      {
        "_index" : "X",
        "_type" : "_doc",
        "_id" : "310165903698515",
        "_score" : null,
        "sort" : [
          "310165903698515"
        ]
      },
      {
        "_index" : "X",
        "_type" : "_doc",
        "_id" : "310165903819494",
        "_score" : null,
        "sort" : [
          "310165903819494"
        ]
      }
    ]
  }
}'''

def json = new JsonSlurper().parseText response

List ids = json.hits.hits*._id

assert ids.toString() == '[310165903526204, 310165903698515, 310165903819494]'

相关问题