0

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 which is within the curly braces. Is there any way so that I can access like array and by doing document.getElementById("Id")=myData[0], so that I don't need to print all elements within the curly braces separately. Please suggest me some solution. Thanks in advance.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
Archana Roy
  • 31
  • 1
  • 8

1 Answers1

0

use var dataArray = JSON.parse(dataString) first, your data is a string at the moment.

Then use dataArray[0]

atinder
  • 2,080
  • 13
  • 15
  • Isn't it array of object? – Archana Roy Aug 26 '15 at 04:26
  • yes it is but it is stringified, you need to parse it first. – atinder Aug 26 '15 at 04:27
  • @ArchanaRoy Apparently not: "*I tried with data[0], but end up with printing "[".*" That sounds like a string to me. – melpomene Aug 26 '15 at 04:33
  • $("#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]. – Archana Roy Aug 26 '15 at 04:43
  • Assign some value form the object not whole object to the html element. Like `document.getElementById("aami").innerHTML= myData[0].datasetName`. – atinder Aug 26 '15 at 07:20
  • Thanks @atinder , I am able to do that, but what my mentor wants me to print data within the curly braces with the help of single variable, like if I consider all elements inside the curly braces as a single element, and then can I access it by any method. – Archana Roy Aug 27 '15 at 05:01
  • `document.getElementById("aami").innerHTML= JSON.stringify(myData[0]);` – atinder Aug 27 '15 at 06:10
  • Here in status I am getting two different data like temp and Hum. If i want to display them seperately how can I do it? – Archana Roy Sep 08 '15 at 06:37