使用谷歌地图API从邮政编码中获取城市,然后将其写入JSON?



我有一个JSON文件,其中包含一堆不同的邮政编码。我想做的是利用谷歌地图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 对象的"测试",现在我想我只需要帮助将其合并到我的 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 数据,然后手动复制/粘贴它,而不是依靠服务器端代码为我完成。这让我想到了这个解决方案:

$(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 格式的单个数组?

您不能直接从浏览器写入文件。可以创建 Blob 或数据 URL,并提供可从中下载文件的链接,如本例所示。

或者,既然你有 Node.js,也许只是在 shell 中运行一个脚本。此示例需要npm install request request-promise-native,但您可以改用 Node.js https API。此外,示例中没有错误处理。

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));
});

最新更新