7

次の JSON から、postal_code (long_name または short_name) を抽出したいと思います。JsonSlurper を使用して変数に取り込み、find/contains/etc を使用してさまざまなクエリを試しました。「タイプ」に「postal_code」が含まれているが、それを把握できていないノードを取得します。どんな助けでも大歓迎です。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Jefferson Ave",
               "short_name" : "Jefferson Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "North Newport News",
               "short_name" : "North Newport News",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Newport News",
               "short_name" : "Newport News",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Virginia",
               "short_name" : "VA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "23608",
               "short_name" : "23608",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Jefferson Ave & Denbigh Blvd, Newport News, VA 23608, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.13852930,
               "lng" : -76.52013079999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.13987828029151,
                  "lng" : -76.51878181970848
               },
               "southwest" : {
                  "lat" : 37.13718031970851,
                  "lng" : -76.52147978029149
               }
            }
         },
         "types" : [ "intersection" ]
      }
   ],
   "status" : "OK"
}
4

1 に答える 1

14

以下は、postal_code タイプのノードを見つける必要があります。resultsまたはこれまでに複数のリスト項目がある場合address_componentsは、インデックス付きアクセスをいくつかの反復に置き換えることでそれに応じて調整する必要がありますが、これが役立つことを願っています.

import groovy.json.*

def text = '''
{
   "results" : [
<omitted rest to save space>
....
}
'''

def json = new JsonSlurper().parseText(text)

def theNode = json.results[0]
                  .address_components
                  .find { it.types[0] == 'postal_code' }

assert '23608' == theNode.long_name
于 2012-09-11T23:15:16.453 に答える