[{"id":7,"message":"This is another test message","taker_id":"131","giver_id":"102","status":"0","stamp":"2016-08-11"}]
That's my response. I try to get a datum. I have tried data.id
but it fails and returns undefined
.
[{"id":7,"message":"This is another test message","taker_id":"131","giver_id":"102","status":"0","stamp":"2016-08-11"}]
That's my response. I try to get a datum. I have tried data.id
but it fails and returns undefined
.
- Sorry same try console.log(data[0].id); Undefined ?! – ROB ENGG Commented Aug 11, 2016 at 15:11
-
2
My you have to use
data = JSON.parse(data);
first – JustOnUnderMillions Commented Aug 11, 2016 at 15:13 - Possible duplicate of php get values from json encode – John Conde Commented Oct 1, 2016 at 19:30
4 Answers
Reset to default 8As I assume that you are working with a JSON string, you first have to parse the string into and JSON object. Else you couldn't reach any of the properties.
parsedData = JSON.parse(data);
Then you can get your property:
parsedData[0].id
This seems to work just fine
var data = [{
"id":7,
"message":"This is another test message",
"taker_id":"131",
"giver_id":"102",
"status":"0",
"stamp":"2016-08-11"
}];
console.log(data[0].id);
https://jsbin./jewatakize/
if you just want to get the id from this one object then data[0].id will work just fine. If you expect to have multiple objects in that same array then you can use a loop. for example if this is angular you can do:
<div ng-repeat='info in data'>
<p>{{info.id}}</p>
</div>
This will allow you to iterate through multiple objects within the array and get all id's.
The problem here is that you have here an array of objects, and you are trying to access it without indexing. You should first parse it using and then access the object by indexing
let objects = JSON.parse(data)
console.log(objects[0].id)