使用jQuery客户端代码读取json对象
我从web服务返回了以下对象:使用jQuery客户端代码读取json对象,jquery,json,Jquery,Json,我从web服务返回了以下对象: { "temperature": { "trend": "-1", "critical_in": "-1", "avg": "20.7", "status": "1", "curr": "25.0", "advises": [ ] }, "ph":
{
"temperature": {
"trend": "-1",
"critical_in": "-1",
"avg": "20.7",
"status": "1",
"curr": "25.0",
"advises": [
]
},
"ph": {
"trend": "-1",
"critical_in": "-1",
"avg": "8.07",
"status": "0",
"curr": "8.12",
"advises": [
]
},
"nh3": {
"trend": "0",
"critical_in": "-1",
"avg": "0.008",
"status": "0",
"curr": "0.001",
"advises": [
]
},
"light": {
"max_value": "1065.7",
"status": "0",
"curr": "12.1986",
"advises": [
]
}
}
我不确定如何使用jquery阅读它
这是我目前拥有的。我尝试过其他方法,但我似乎无法解决这个问题。我正在尝试获取“温度>平均值”
$.getJSON('https://api.seneye.com/v1/devices/44277/exps?user=xxxxxxx&pwd=xxxxxx,函数(json){
//控制台日志(响应);
log(json);
var statusHTML='”;
$.each(json,函数(i,参数){
statusHTML+='- ;
statusHTML+=参数温度;
statusHTML+='
';
});
状态HTML+='
';
$('#employeeList').html(statusHTML);
}); // 结束获取json
非常感谢您的帮助。在下面的示例中,Success函数中的
result
变量将保存您返回的json字符串
$.ajax({
url: 'http://xxxx.com/mymethod',
data: dataToPost,
method: 'POST',
dataType: 'json',
success: function(result) {
console.log(result.temperature.trend);
console.log(result.ph.trend);
//etc...
},
error: function (xhr, status, err) {
console.log('Error Occured: ' + status)
}
})
您可以通过使用点“”访问json的值
根据给定的JSON字符串,仅当您要迭代
通知
节点时,才需要使用foreach循环。预期的输出是什么?使用如果您想查看示例,则不需要使用each():可能重复:
$.ajax({
url: 'http://xxxx.com/mymethod',
data: dataToPost,
method: 'POST',
dataType: 'json',
success: function(result) {
console.log(result.temperature.trend);
console.log(result.ph.trend);
//etc...
},
error: function (xhr, status, err) {
console.log('Error Occured: ' + status)
}
})