0

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
4
  • Is the server responding with JSON or a string of JSON? Commented Apr 7, 2015 at 18:00
  • this is endpoints. the GAE somehow convert java object to js object Commented 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. Commented Apr 7, 2015 at 18:04
  • well. thanks. i get what was the problem Commented Apr 7, 2015 at 18:07

2 Answers 2

1
// 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
Sign up to request clarification or add additional context in comments.

Comments

0

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]]);
}
answered Apr 7, 2015 at 18:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.