如何合并两个不同的url以使用jquery进行解析
我想尝试使用两个不同的url进行解析,但我的代码不起作用: 我试过两个不同的例子:如何合并两个不同的url以使用jquery进行解析,jquery,json,parsing,getjson,Jquery,Json,Parsing,Getjson,我想尝试使用两个不同的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=jso
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);
});
});
您可以在jsfiddle()上尝试该代码。您所做的唯一错误是假设您正在接收的数据是 一个简单的
console.log(data1)
就足以告诉您,在data1
中接收到的数据不是来自URL的数据,而是包含更多信息的数组
您所需要的只是指向正确的阵列,所有阵列都将按照您的要求工作:
var url=”https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
变量url2=”https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";
$.when($.getJSON(url),$.getJSON(url2)).done(函数(data1,data2){
警报(数据1[0]。地址。村庄);
警报(数据2[0]。地址。城市);
//使用“数据”和“数据2”进行操作
});
您可以轻松检查数据1中的内容。。。这是一个数组,不是URL中的对象。。。您的警报应该是data1[0]。address.village
和data2[0]。address.city
,因为data1和data2来自哪里?您可能想了解如何使用getJson()
函数。您应该了解异步和同步调用的定义。。。你的代码永远不会像你在答案中那样有效<代码>控制台。日志(数据)
将始终是未定义的
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