Server returns me such object, but i need only array ITEMS.
How can i get it?
I tried array['items'] but the result is undefiend
{
"items": [
{
..
},
{
..
},
{
..
}
],..
,..
}
Necreaux
9,8348 gold badges29 silver badges43 bronze badges
asked Apr 7, 2015 at 17:57
Oleh Kurpiak
1,3691 gold badge16 silver badges39 bronze badges
-
Is the server responding with JSON or a string of JSON?tcigrand– tcigrand2015年04月07日 18:00:52 +00:00Commented Apr 7, 2015 at 18:00
-
this is endpoints. the GAE somehow convert java object to js objectOleh Kurpiak– Oleh Kurpiak2015年04月07日 18:02:08 +00:00Commented Apr 7, 2015 at 18:02
-
Could you post more of the code? From what you've posted so far, it looks like what you're trying to do should work.tcigrand– tcigrand2015年04月07日 18:04:30 +00:00Commented Apr 7, 2015 at 18:04
-
well. thanks. i get what was the problemOleh Kurpiak– Oleh Kurpiak2015年04月07日 18:07:50 +00:00Commented Apr 7, 2015 at 18:07
2 Answers 2
// JSON string returned from the server
var text = '{"items":[{"name":"itemX" ,"price":15},{"name":"itemY","price":25},{"name":"itemZ","price":20}]}';
// Convert the string returned from the server into a JavaScript object.
var object = JSON.parse(text);
// Accessing a specific property value of an item
console.log(object.items[0].name); //itemX
// Extract 'items' in to a separate array
var itemsArray = object.items;
console.log(itemsArray); //[itemObject, itemObject, itemObject]
answered Apr 7, 2015 at 19:11
Sasindu Mendis
3562 silver badges7 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you're getting this as a string:
var json = JSON.parse(my_json_string)
Then,
var keys = Object.keys(json);
var values = [];
for(var i = 0; i < keys.length; i++){
values.push(json[keys[i]]);
}
Comments
lang-js