Javascript http请求并转换为json,并与json进行比较 我想向url发出http请求,比如 这将为我提供xml 我想皈依 这个从1到json的xml可以说是json 1 现在我向url let发出一个rest请求 假设它返回一个json,假设json 2
json 1 [ 现在Id是json1和json2之间的常量,我想生成一个json结果,如下所示,让我们调用json3Javascript http请求并转换为json,并与json进行比较 我想向url发出http请求,比如 这将为我提供xml 我想皈依 这个从1到json的xml可以说是json 1 现在我向url let发出一个rest请求 假设它返回一个json,假设json 2,javascript,Javascript,json 1 [ 现在Id是json1和json2之间的常量,我想生成一个json结果,如下所示,让我们调用json3 [ { "id": "123", "testname": "test123", "name": "John Doe", "active": true, "type": "test6", "country":"USA", "state":"KA" }
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6",
"country":"USA",
"state":"KA"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7",
"country":"UK",
"state":"MA"
}
]
现在我尝试了1和3,但2不知道该做什么,比较和结合,我不确定该做什么,如果有任何帮助将是巨大的
function fun()
{
var data="hello";
$.post('http://localhost/ws/service.asmx/HelloWord',{},function(response)
{ data = response;
}).error(function(){
alert("Sorry could not proceed");
});
return data;
}
您可以尝试以下方法:
<script>
$(document).ready(function(){
var result=[];
var x=[{"id": "123","testname": "test123", "name": "John Doe","active": true,"type": "test6"},{"id": "456", "testname": "test564", "name": "Ship Therasus", "active": true,"type": "test7" }];
var y = [{ "id": "123", "country": "USA", "state": "KA", "age": 24,"group": "g1"},{ "id": "456", "country": "UK", "state": "MA", "age": 28, "group": "G2"}];
for (i in x){
for (j in y){
if(x[i].id == y[j].id){
result.push($.extend(x[i], y[j]));
}
}
}
console.log(result);
});
</script>
$(文档).ready(函数(){
var结果=[];
var x=[{“id”:“123”,“testname”:“test123”,“name”:“John Doe”,“active”:true,“type”:“test6”},{“id”:“456”,“testname”:“test564”,“name”:“Ship Therasus”,“active”:true,“type”:“test7”}];
变量y=[{“id”:“123”,“国家”:“美国”,“州”:“KA”,“年龄”:24,“集团”:“g1”},{“id”:“456”,“国家”:“英国”,“州”:“MA”,“年龄”:28,“集团”:“G2”}];
对于(x中的i){
对于(j in y){
如果(x[i].id==y[j].id){
结果.push($.extend(x[i],y[j]);
}
}
}
控制台日志(结果);
});
<script>
$(document).ready(function(){
var result=[];
var x=[{"id": "123","testname": "test123", "name": "John Doe","active": true,"type": "test6"},{"id": "456", "testname": "test564", "name": "Ship Therasus", "active": true,"type": "test7" }];
var y = [{ "id": "123", "country": "USA", "state": "KA", "age": 24,"group": "g1"},{ "id": "456", "country": "UK", "state": "MA", "age": 28, "group": "G2"}];
for (i in x){
for (j in y){
if(x[i].id == y[j].id){
result.push($.extend(x[i], y[j]));
}
}
}
console.log(result);
});
</script>