使用Google Maps API从Zip代码中获取城市,然后将其写入JSON?

我有一堆不同的邮政编码的JSON文件。 我想要做的是利用Google Maps API检索与邮政编码相关联的城市,然后将其写入JSON文件。

到目前为止,我有一个正确的从结果中抓住城市的脚本。 我将如何去写这些城市的JSON文件?

这是我的JSON数据:

[ { "State": "Oklahoma", "ZIP": 73169 }, { "State": "Oklahoma", "ZIP": 73169 }, { "State": "Oklahoma", "ZIP": 73170 }, { "State": "Oklahoma", "ZIP": 73172 }, { "State": "Oklahoma", "ZIP": 73173 } ] 

这是我的脚本:

 $(function() { $.get('locations.json', function(data) { $.each(data, function(i, item) { var zip = item.ZIP; var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&key=AIzaSyD_YqB4d_-xKcmNP9jJCiPkJYDS8J3f6pI"; $.get(url, function(loc) { for(var i=0;i<loc.results.length;i++) { var address = loc.results[i].formatted_address; var city = address.split(',', 1)[0] console.log(city); } }); }); }); }); 

这是一个笨蛋

更新:

我发现一种方法来写这个问题的 NodeJS我的JSON文件,下面的代码成功地更新了“城市”为每个我的JSON对象“testing”,现在我想我只需要帮助将其纳入我的jQuery代码:

 fs = require('fs'); var m = JSON.parse(fs.readFileSync('locations.json').toString()); m.forEach(function(p){ p.City = "test"; }); fs.writeFile('locations.json', JSON.stringify(m)); 

如果这会更容易,我也愿意使用PHP。

另一个尝试(最接近我想要做的):

计划是只用这个脚本来填充所有城市的JSON。 所以我想我会尝试做到这一点在客户端,我可以只注销一些JSON数据,然后手动复制/粘贴,而不是依靠服务器端代码为我做。 这让我想到了这个解决scheme:

 $(function() { var item = []; var locations = [ { "State": "Oklahoma", "ZIP": "73169", "City": "" }, { "State": "Oklahoma", "ZIP": "73169", "City": "" }, { "State": "Oklahoma", "ZIP": "73170", "City": "" }, { "State": "Oklahoma", "ZIP": "73172", "City": "" }, { "State": "Oklahoma", "ZIP": "73173", "City": "" } ]; $.each(locations, function(i, item) { var zip = item.ZIP; var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&key=AIzaSyA75wif8_yewifGSqVFnR3U50zuW_EnAns"; $.get(url, function(loc) { for(var i=0;i<loc.results.length;i++) { var address = loc.results[i].formatted_address; var city = address.split(',', 1)[0]; item.City = city; console.log(item); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

如果我在for语句中注销item ,它将正确返回所有内容。 我怎么能输出这个JSON格式的单个数组?

您不能直接从浏览器写入文件。 您可以创build一个blob或一个数据URL,并提供一个链接,从这个例子中可以下载文件。

或者,既然你有Node.js,也许只是在shell中运行一个脚本。 这个例子需要npm install request request-promise-native ,但是你可以使用Node.js https API 。 另外,在示例中没有error handling。

 const fs = require('fs'); const http = require('http'); const request = require('request-promise-native'); const baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?key=<your key>&address='; const locations = JSON.parse(fs.readFileSync('locations.json', 'utf8')); Promise.all(locations.map((location) => { return request .get(baseUrl + location.ZIP) .then((result) => { location.city = JSON.parse(result).results[0].formatted_address.split(',', 1)[0]; return location; }); })).then((results) => { fs.writeFileSync('results.json', JSON.stringify(results)); }); 

编辑:我刚刚阅读你的评论甚至复制粘贴足够你。 你也可以在浏览器中使用上面的方法,不需要jQuery:

 Promise.all(locations.map((location) => { return fetch(baseUrl + location.ZIP) .then((result) => { location.city = result.results[0].formatted_address.split(',', 1)[0]; return location; }); })).then((results) => { console.log(JSON.stringify(results)); });