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: |
$.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]
Hemlata KushwahaPosted Aug 26, 2015, 5:56 AM
Upendra Pratap ShahiPosted Aug 27, 2015, 2:10 AM
Upendra Pratap ShahiPosted Aug 27, 2015, 1:08 AM
Archana RoyPosted Aug 27, 2015, 1:05 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 8:23 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 5:57 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 5:46 AM
Archana RoyPosted Aug 26, 2015, 5:41 AM
Hemlata KushwahaPosted Aug 26, 2015, 5:36 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 5:27 AM
function GetDayWiseDetail_Test() {
var Status;
$.ajax({
type: "POST",
url: ResolveUrl("~/WebMethods.aspx/GetDayWiseDetail_test"),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
Status = data.d;
alert(Status)
},
failure: function (errMsg) {
}
});
return Status;
}
if you want to alert then use above oralert(data.Id);//Id is json key which return
Archana RoyPosted Aug 26, 2015, 5:21 AM
Archana RoyPosted Aug 26, 2015, 5:20 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 5:17 AM
Manas MohapatraPosted Aug 26, 2015, 4:56 AM
Archana RoyPosted Aug 26, 2015, 4:48 AM
Archana RoyPosted Aug 26, 2015, 4:45 AM
Manas MohapatraPosted Aug 26, 2015, 3:42 AM
Upendra Pratap ShahiPosted Aug 26, 2015, 3:41 AM