使用jQuery客户端代码读取json对象

时间:2017-03-01 09:01:00

标签: jquery json

我从Web服务返回以下对象:

{
        "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', function (json) {
            // console.log(response);
            console.log(json);
            var statusHTML = '<ul class="bulleted">';
            $.each(json, function (i, params) {

                statusHTML += '<li class="out">';

                statusHTML += params.temperature;
                statusHTML += '</li>';
            });
            statusHTML += '</ul>';
            $('#employeeList').html(statusHTML);
        }); // end get json

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在以下示例中,Success Function中的dataArray变量将保存您返回的json字符串。

result

只需使用点“。”即可访问json的值。

根据给定的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) } }) 节点时才需要使用foreach循环。