var text = '{ "employees" : [' +
'{ "id": 999, "username": "Batman" } ]}';
obj = JSON.parse(text);
var id = obj.employees[0].id;
Question is, how to access as in example above id, from javascript object? In example above it has "name" to gain access to id, which is employees. But real life example below i don't have "name" to use to gain id value.
var text = '{ "" : [' +
'{ "id": 999, "username": "Batman" } ]}';
obj = JSON.parse(text);
var id = obj.**WHAT TO PUT HERE TO ACCESS-->**.id;
var text = '{ "employees" : [' +
'{ "id": 999, "username": "Batman" } ]}';
obj = JSON.parse(text);
var id = obj.employees[0].id;
Question is, how to access as in example above id, from javascript object? In example above it has "name" to gain access to id, which is employees. But real life example below i don't have "name" to use to gain id value.
var text = '{ "" : [' +
'{ "id": 999, "username": "Batman" } ]}';
obj = JSON.parse(text);
var id = obj.**WHAT TO PUT HERE TO ACCESS-->**.id;
Share
Improve this question
asked Jun 9, 2015 at 22:17
JohnJohn
451 silver badge6 bronze badges
0
2 Answers
Reset to default 7you can access for empty string as a key and remember that is a array inside, use the item 0
obj[''][0].id
You can avoid this situation by using an array of objects (removing the external curly braces) like
var text = '[{ "id": 999, "username": "Batman" } ]';
objs = JSON.parse(text);
var id = objs[0].id;
console.log(id); // 999