0

I have the following object being returned from a web service:

{
        "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": [

            ]
        }
    }    

I'm unsure on how to read it using jquery?

Here is what I currently have. I've tried other things but I cannot seem to figure this out. I'm trying to get the "Temperature > avg"

$.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

Any help is appreciated.

Tim
  • 563
  • 2
  • 6
  • 20

1 Answers1

1

In the following example, result variable in Success Function will hold your returned json string.

$.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)
    }

})

You can access to the values of json by just using dot "."

According to the given JSON String, You will need to use foreach loop only when you want to iterate advises node.

TTCG
  • 8,805
  • 31
  • 93
  • 141