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": [
    {
      ..
    },
    {
      ..
    },
    {
      ..
    }
  ],.. 
  ,..
}
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]
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]]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.