Reputation: 113
If I have this:
{
"data": [{
"id": "4831",
"name": "Rui Vasco Martins",
"nickname": "rvgmartins",
"money_start": "10200",
"balance": "8000"
}]
}
How do I get the value of data->id
, data->name
, etc ?
Upvotes: 4
Views: 26095
Reputation: 76444
First, let's understand your problem. Notes to be taken:
foo
data->id
in Javascript. If you have an object called data
and it has a member called id
, then you reference it as data.id
name
is inside an array. An element of an array is referenced by an index, which starts from 0, so, your array noted as data
is an array having a single element, which is the 0th elementSo, you can get the id
with foo.data[0].id
and you can get the name
as foo.data[0].name
. However, as per se, nothing forces your array to have exactly one element. If there are more elements, you need to be able to decide somehow which element's id
and name
you are interested in. Also, your array is not forced to have elements at all.
Whenever you see such a JSON, arrays are enclosed inside []
brackets and objects are enclosed inside {}
brackets.
Upvotes: 0
Reputation: 1357
Your object contains an array, so you need to use the correct indices to further delve into the object; your id and name happen to be in the first array, so we use obj.data[0]
.
var obj = {"data":[{"id":"4831","name":"Rui Vasco Martins","nickname":"rvgmartins","money_start":"10200","balance":"8000","profit":"5010","nreload":"1","deal_id":"4813","created_at":"2015-05-27 12:54:33","updated_at":"2015-07-21 22:31:47"}],"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"services/dashboard","headers":{"Accept":"application/json, text/pla`in, */*"}},"statusText":"OK"}
You can use either dot or square bracket notation, to further identify the object property, here's dot notation:
var data_id = obj.data[0].id; // "4831"
var data_name = obj.data[0].name; // "Rui Vasco Martins"
Here's square-bracket notation:
var data_id = obj.data[0]['id']; // "4831"
var data_name = obj.data[0]['name']; // "Rui Vasco Martins"
Upvotes: 4
Reputation: 3034
As within data
there is an array, you can do:
var myJson = {
"data": [{
"id": "4831",
"name": "Rui Vasco Martins",
"nickname": "rvgmartins",
"money_start": "10200",
"balance": "8000",
"profit": "5010",
"nreload": "1",
"deal_id": "4813",
"created_at": "2015-05-27 12:54:33",
"updated_at": "2015-07-21 22:31:47"
}],
"status": 200,
"config": {
"method": "GET",
"transformRequest": [null],
"transformResponse": [null],
"url": "services/dashboard",
"headers": {
"Accept": "application/json, text/plain, */*"
}
},
"statusText": "OK"
}
document.write('id: ' + myJson.data[0].id + '</br>name: ' + myJson.data[0].name);
Upvotes: 0
Reputation: 1905
Data is multiple. It means it is an array of objects. There's no such things as data->id, only data[ n ]->id. In that case, you might want all possible IDs and you can do it with a traditionnal for loop, or other.
var i =0;
var total = object.data.length;
var ids = [];
for(; i < total; i++) {
ids.push( data[ i ].id );
}
console.log(ids);
If you are SURE there's only 1 data entry and will always be, you can be straight forward and go object.data[ 0 ].id
as well!
Please note that "object" is the variable containing the JSON :)
And for next time, here's a little hint : http://jsonlint.com/
Hope that helps!
Upvotes: 4