Archana Roy

Archana Roy

  • NA
  • 13
  • 1.1k

Access to elements of JSON array of objects

Aug 26 2015 3:11 AM
My data on server is in the form of JSON array of objects. If I want to print the first object, how can I access its first object? Format of data is:
 
 
[ { "eventId": "8577",
 "datasetId": "34",
"nodeId": "8076",
 "typeId": "4",
"type": "TempAndHum",
 "status": "Temp : 35, Hum : 83",
 "datasetName": "test active set",
 "mode": "shared",
"xmlFragment": "Absent",
"gpsLat": "-23.549999",
 "gpsLong": "-46.633301",
 "contributor": "SanatIITD",
 "addedOn": "2015-04-21 08:03:16",
 "updatedOn": "2015-04-21 08:03:16" },

 { "eventId": "8576",
 "datasetId": "34",
 "nodeId": "8076",
"typeId": "4",
"type": "TempAndHum",
"status": "Temp : 34, Hum : 81",
 "datasetName": "test active set",
 "mode": "shared",
 "xmlFragment": "Absent",
"gpsLat": "-23.549999",
 "gpsLong": "-46.633301",
 "contributor": "SanatIITD",
"addedOn": "2015-04-21 08:03:11",
"updatedOn": "2015-04-21 08:03:11" },

 { "eventId": "8575",
 "datasetId": "34",
 "nodeId": "8076",
 "typeId": "4",
"type": "TempAndHum",
"status": "Temp : 33, Hum : 80",
 "datasetName": "test active set",

"mode": "shared",
 "xmlFragment": "Absent",
"gpsLat": "-23.549999",
 "gpsLong": "-46.633301",
 "contributor": "SanatIITD",
 "addedOn": "2015-04-21 08:03:05",
 "updatedOn": "2015-04-21 08:03:05" },
]
I tried with data[0], but end up with printing "[". I tried with JSON.stringify also and then split with "," but that gives me first element
 of the object, not the whole first object. I need to print the whole first object. Please suggest me some solution. Thanks in advance.

Part of my code is:
$("#submit").click(function(){
$.ajax({ type: 'GET', url: "resource.php",
success:function(data){
alert(data);
var myData = JSON.parse(data);
document.getElementById("aami").innerHTML= myData[0]; }
});
Not getting where I am wrong? When I run this code I am getting [ object object]
 

Answers (18)