解析GoogleMapsJSON数据以在JQ(而不是JQuery)中进行地理编码

解析GoogleMapsJSON数据以在JQ(而不是JQuery)中进行地理编码,json,google-maps,jq,Json,Google Maps,Jq,我正试图通过JQ从Lat和Long值中获取国家和城市名称 下面是完整的JSON示例 我贴上了 尝试选择国家和城市名称,但最接近的是 .results[0].address_components[].short_name 如何指定仅将节点带到“类型”:[“国家”、“政治”] 谢谢我写了一个函数来实现这一点 /** * geocodeResponse is an object full of address data. * This function will "fish" for

我正试图通过JQ从Lat和Long值中获取国家和城市名称

下面是完整的JSON示例

我贴上了

尝试选择国家和城市名称,但最接近的是

.results[0].address_components[].short_name
如何指定仅将节点带到
“类型”:[“国家”、“政治”]


谢谢

我写了一个函数来实现这一点

/**
*   geocodeResponse is an object full of address data.  
*   This function will "fish" for the right value
*   
*   example: type = 'postal_code' => 
*   geocodeResponse.address_components[5].types[1] = 'postal_code'
*   geocodeResponse.address_components[5].long_name = '1000'
* 
*   type = 'route' => 
*   geocodeResponse.address_components[1].types[1] = 'route'
*   geocodeResponse.address_components[1].long_name = 'Wetstraat'
*/
function addresComponent(type, geocodeResponse, shortName) {
  for(var i=0; i < geocodeResponse.address_components.length; i++) {
    for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
      if (geocodeResponse.address_components[i].types[j] == type) {
        if (shortName) {
          return geocodeResponse.address_components[i].short_name;
        }
        else {
          return geocodeResponse.address_components[i].long_name;
        }
      }
    }
  }
  return '';
}

我在这里使用了它:

将json分配给results变量var results={your json}。 然后试试这个:

for( var idx in results.results)
{
    var address = results.results[idx].address_components;
    for(var elmIdx in address)
    {
        if(address[elmIdx].types.indexOf("country") > -1 &&
           address[elmIdx].types.indexOf("political") > -1)
        {
           address[elmIdx].short_name //this is the country name
           address[elmIdx].long_name  //this is the country name

        }
    }
}    

我不清楚你到底在找什么。每个结果都有一组类型,每个地址组件也有一组类型。你想要哪一个?我们可以编写一个过滤器,它将匹配您尝试的内容,但考虑到数据,它将对您完全无用。唯一包含您列出的类型的项目只是一个国家/地区名称

无论如何,假设您想要获得类型为
“country”
“political”
的结果对象,请使用
contains()
过滤器

.results | map(
    select(
        .types | contains(["country","political"])
    )
)

否则,您需要澄清您到底想要从这个数据集中得到什么。一个预期结果的例子…

谢天谢地,有人已经问过并回答了这个问题,否则我就不会使用exif GPS json数据和这些复杂的规则来组织我的照片了。这仍然有效吗?对我来说,它只是返回[]
.results | map(
    select(
        .types | contains(["country","political"])
    )
)