Outputting a record from JSON format to a specific position

I use the google geocoding API to retrieve data based on a zip code, which I pass as follows:

http://maps.googleapis.com/maps/api/geocode/json?address=97001&sensor=true

I get this result:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "97001",
               "short_name" : "97001",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Antelope",
               "short_name" : "Antelope",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Oregon",
               "short_name" : "OR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Antelope, OR 97001, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 45.0835979,
                  "lng" : -120.476616
               },
               "southwest" : {
                  "lat" : 44.801399,
                  "lng" : -120.91835
               }
            },
            "location" : {
               "lat" : 44.9552895,
               "lng" : -120.6265837
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.0835979,
                  "lng" : -120.476616
               },
               "southwest" : {
                  "lat" : 44.801399,
                  "lng" : -120.91835
               }
            }
         },
         "place_id" : "ChIJhZMDGc0ovFQRBev1yy22nAk",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

Now this is good, but I would like to select only the entry from "addres_component", where the types are equal:

"types" : [ "administrative_area_level_1", "political" ]

I can do something like this to get the value:

JObject.Parse(myJson)["keyhere"].ToString();

But how do I really check if the type for the short_name and long_name values ​​is what I set above?

How can I achieve this with the JObject class?

+4
source share
1 answer

Newtonsoft JSON , SelectToken, JSONPath ( XPath) JSON.

: http://www.newtonsoft.com/json/help/html/SelectToken.htm

- :

JObject o = JObject.Parse(json); 
JToken token = o.SelectToken("$..address_components[?(@.types.indexOf('administrative_area_level_1') != -1), ?(.types.indexOf('political') != -1)]");

, , , . Newtonsoft JSON.

JSONPath :

'0' ...
  'long_name' => "Oregon"
  'short_name' => "OR"
  'types' ...
    '0' => "administrative_area_level_1"
    '1' => "political"

:

?() JSONPath JavaScript. : fooobar.com/questions/1665610/...

, OR. JSONPath . : fooobar.com/questions/969927/...

JSONPath -. http://jsonpath.com/

+3

Source: https://habr.com/ru/post/1665609/


All Articles