Search code examples
jqueryjsonjquery-templates

How to retrive data for n array of objects in jquery templates?


var json = [{
    "id": "702",
    "nid": "1",
    "node_count": "211",
    "sdata": "100",
    "stype": "1",
    "timestamp": "2014-04-04 09:00:42"
}][{
    "id": "697",
    "nid": "2",
    "node_count": "218",
    "sdata": "0",
    "stype": "1",
    "timestamp": "2014-04-0    4 08:41:05"
}][{
    "id": "700",
    "nid": "3",
    "node_count": "217",
    "sdata": "0",
    "stype": "1",
    "timestamp": "2014-04-04 08:41:52"
}][{
    "id": "699",
    "nid": "4",
    "node_count": "33",
    "sdata": "0",
    "stype": "1",
    "timestamp": "2014-0    4-04 08:41:46"
}][{
    "id": "701",
    "nid": "5",
    "node_count": "23",
    "sdata": "0",
    "stype": "1",
    "timestamp": "2014-04-04 08:42:20"
}]
$.each(json, function (arrayID, SensorData) {

    alert('Node id' + SensorData.nid);
    alert('SensorData' + SensorData.sdata);
    alert('Sensor  Type' + SensorData.stype);
    alert('Time is ' + SensorData.timestamp);

});

Solution

  • The json you are looking for should be something like this.

    var json = [{
        "id": "702",
        "nid": "1",
        "node_count": "211",
        "sdata": "100",
        "stype": "1",
        "timestamp": "2014-04-04 09:00:42"
       },
       {
        "id": "697",
        "nid": "2",
        "node_count": "218",
        "sdata": "0",
        "stype": "1",
        "timestamp": "2014-04-0    4 08:41:05"
      },
      {
        "id": "700",
        "nid": "3",
        "node_count": "217",
        "sdata": "0",
        "stype": "1",
        "timestamp": "2014-04-04 08:41:52"
      },
      {
        "id": "699",
        "nid": "4",
        "node_count": "33",
        "sdata": "0",
        "stype": "1",
        "timestamp": "2014-0    4-04 08:41:46"
    },{
        "id": "701",
        "nid": "5",
        "node_count": "23",
        "sdata": "0",
        "stype": "1",
        "timestamp": "2014-04-04 08:42:20"
    }];