使用Google Refine从Google Maps API JSON中提取邮政编码

时间:2013-04-15 22:22:47

标签: json google-maps postal-code google-refine

我正在尝试使用Google Refine从Google Maps API JSON中提取邮政编码。

我通过提取网址添加了一个新列:

"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" + escape(value, "url")

然后生成的JSON如下:

{ "results" : [ { "address_components" : [ { "long_name" : "44", "short_name" : "44", "types" : [ "street_number" ] }, { "long_name" : "Homer Street", "short_name" : "Homer St", "types" : [ "route" ] }, { "long_name" : "London", "short_name" : "London", "types" : [ "locality", "political" ] }, { "long_name" : "Greater London", "short_name" : "Gt Lon", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "United Kingdom", "short_name" : "GB", "types" : [ "country", "political" ] }, { "long_name" : "W1H 4NW", "short_name" : "W1H 4NW", "types" : [ "postal_code" ] }, { "long_name" : "London", "short_name" : "London", "types" : [ "postal_town" ] } ], "formatted_address" : "44 Homer Street, London, Greater London W1H 4NW, UK", "geometry" : { "location" : { "lat" : 51.51981750, "lng" : -0.16534040 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 51.52116648029151, "lng" : -0.1639914197084980 }, "southwest" : { "lat" : 51.51846851970851, "lng" : -0.1666893802915020 } } }, "types" : [ "street_address" ] } ], "status" : "OK" }

在浏览了几个博客以找到相关代码后,我尝试使用此转换列...

value.parseJson().results[0]["formatted_address"]

...这对于完整地址非常有用。

当我尝试提取邮政编码时会出现问题。我试着摆弄并无处可去,然后我下载了JSONPad并将JSON粘贴到树形图中以获得路径:

value.parseJson().results[0]["address_components"][5]["long_name"]

问题在于,这为某些条目完美地提取了邮政编码,而对其他人来说则不那么完美,例如,它提取了其他东西 - 城镇或国家。

将[5]改为[6]似乎提取了其他地址的邮政编码,但有没有办法只提取邮政编码,无论它在结构中的位置如何?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您可能需要做的是遍历address_components数组中的结构,检查每个结构的“类型”。当类型包含“postal_code”,然后是tada,这是你的邮政编码。

类似下面的代码(对我有用):

<script type="text/javascript">
var stuData = 
{
    "results": [{
            "address_components": [{
                    "long_name": "44",
                    "short_name": "44",
                    "types": ["street_number"]
                }, {
                    "long_name": "Homer Street",
                    "short_name": "Homer St",
                    "types": ["route"]
                }, {
                    "long_name": "London",
                    "short_name": "London",
                    "types": ["locality", "political"]
                }, {
                    "long_name": "Greater London",
                    "short_name": "Gt Lon",
                    "types": ["administrative_area_level_2", "political"]
                }, {
                    "long_name": "United Kingdom",
                    "short_name": "GB",
                    "types": ["country", "political"]
                }, {
                    "long_name": "W1H 4NW",
                    "short_name": "W1H 4NW",
                    "types": ["postal_code"]
                }, {
                    "long_name": "London",
                    "short_name": "London",
                    "types": ["postal_town"]
                }
            ],
            "formatted_address": "44 Homer Street, London, Greater London W1H 4NW, UK",
            "geometry": {
                "location": {
                    "lat": 51.51981750,
                    "lng": -0.16534040
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 51.52116648029151,
                        "lng": -0.1639914197084980
                    },
                    "southwest": {
                        "lat": 51.51846851970851,
                        "lng": -0.1666893802915020
                    }
                }
            },
            "types": ["street_address"]
        }
    ],
    "status": "OK"
};

var myPostcode;

for (var i = 0; i < stuData.results[0].address_components.length; i++) {
    for (var j = 0; j < stuData.results[0].address_components[i].types.length; j++) {
        if (stuData.results[0].address_components[i].types[j] == "postal_code") {
            myPostcode = stuData.results[0].address_components[i].long_name;
            break;
        }
    }
}

console.log(myPostcode);
</script>

答案 1 :(得分:0)

您可以将duncan的答案从Javascript翻译为Python并将其与Jython解释器一起使用,或者您可以使用以下GREL表达式(我发布到Google Groups以回应您的查询):

  

过滤器(的forEach(value.parseJson()。结果[0] .address_components,C,如果(c.types [0] == 'POSTAL_CODE',c.long_name, '')),V,V'!= '')[0]

当然,根据API的服务条款,这一切都假定您使用此数据在Google地图上显示。