【问题标题】:Taking out a record from JSON format on specific position从 JSON 格式中取出特定位置的记录
【发布时间】:2017-01-03 12:41:12
【问题描述】:

我正在使用谷歌的地理编码 API 根据我传递的邮政编码获取数据,如下所示:

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

我得到这样的结果:

{
   "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"
}

现在这很好,但我只想从“addres_component”中挑选类型等于的记录:

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

我可以这样做来获取值:

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

但是我如何实际检查值 short_name 和 long_name 的类型是否是我在上面放置的类型?

如何使用 JObject 类实现这一点?

【问题讨论】:

    标签: c# asp.net json asp.net-mvc geocode


    【解决方案1】:

    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。在此处查看详细信息:https://stackoverflow.com/a/30031333/1862405

    联合字符 , 的作用类似于 OR。 JSONPath 中还没有逻辑运算符。在这里阅读更多:https://stackoverflow.com/a/23897735/1862405

    为了测试您的 JSONPath,您可以使用在线测试仪。我用http://jsonpath.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2014-07-13
      相关资源
      最近更新 更多