我在解析这个JSON输出时遇到了一些困难,我从请求中得到了这个输出。我想要完成的是显示两种类型的输出。
我想只显示 USER1"更改"和"时间" 属性。
我想展示所有用户"更改"和"时间"和"日期" 属性。
以下是json的样子。
[
{"@attributes":
{"name":"USER1","last":"72.34","change":"-0.04","sum":"-0.06","volume":"963188","height":"74.69","low":"58.68","date":"Mar 10 2014","time":"04:00"}
},
{"@attributes":
{"name":"USER2","last":"63.98","change":"0.37","sum":"0.58","volume":"1191173","height":"66.75","low":"55.1","date":"Mar 10 2014","time":"04:00"}
},
{"@attributes":
{"name":"USER3","last":"94.56","change":"0.62","sum":"0.66","volume":"1136925","height":"94.86","low":"73.89","date":"Mar 10 2014","time":"04:00"}
}
]
有人可以帮助我开始如何处理这个问题吗?非常感谢。
感谢您抽出宝贵时间阅读本文。
答案 0 :(得分:1)
您需要使用ajax来解决此问题。试试这个我的朋友
仅显示USER1“更改”和“时间”属性
formData = {
name: "USER1"
}
$.ajax({
type: 'GET',
contentType: 'application/json',
url: "https://example.com/detail.php",
dataType: "json",
data: formData,
success: function(data) {
console.log(data.@attributes[0].change);
console.log(data.@attributes[0].time);
},
error: function(data) {
//error handler
}
});
显示所有用户“更改”,“时间”和“日期”属性。
$.ajax({
type: 'GET',
contentType: 'application/json',
url: "https://example.com/all.php",
dataType: "json",
success: function(data) {
$.each(data.@attributes, function(index, item) {
console.log(item.change);
console.log(item.time);
console.log(item.date);
});
},
error: function(data) {
//error handler
}
});
完成!!