如何使用 jquery 合并两个不同的 url 进行解析
How to merge two different url for parsing using jquery
我想尝试用两个不同的 url 进行解析,但我的代码不起作用:
我尝试了两个不同的例子:
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {
alert(data1.address.village);
alert(data2.address.city);
//do stuff with 'data' and 'data2'
});
$.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data) {
$.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data2) {
var concatenatedJson = $.extend({}, data, data2);
alert(concatenatedJson.address.road);
});
});
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
var data = {};
$.getJSON(url, function(result){
data.data1 = result;
});
$.getJSON(url2, function(result){
data.data2 = result;
});
console.log(data); // will return the two urls json code
您可以在 jsfiddle 上尝试代码 (https://jsfiddle.net/qtcvux31/)
您唯一的错误是假设您正在接收的数据
一个简单的console.log(data1)
就足以告诉你在data1
中接收到的数据不是来自URL的数据,而是一个包含更多信息的数组
您所需要的只是指向正确的数组,一切都会按照您的意愿工作:
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {
alert(data1[0].address.village);
alert(data2[0].address.city);
//do stuff with 'data' and 'data2'
});
我想尝试用两个不同的 url 进行解析,但我的代码不起作用: 我尝试了两个不同的例子:
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {
alert(data1.address.village);
alert(data2.address.city);
//do stuff with 'data' and 'data2'
});
$.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data) {
$.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data2) {
var concatenatedJson = $.extend({}, data, data2);
alert(concatenatedJson.address.road);
});
});
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
var data = {};
$.getJSON(url, function(result){
data.data1 = result;
});
$.getJSON(url2, function(result){
data.data2 = result;
});
console.log(data); // will return the two urls json code
您可以在 jsfiddle 上尝试代码 (https://jsfiddle.net/qtcvux31/)
您唯一的错误是假设您正在接收的数据
一个简单的console.log(data1)
就足以告诉你在data1
中接收到的数据不是来自URL的数据,而是一个包含更多信息的数组
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {
alert(data1[0].address.village);
alert(data2[0].address.city);
//do stuff with 'data' and 'data2'
});