服务器返回我这样的对象,但我只需要数组ITEMS。
我怎么才能得到它?
我尝试了array['items'],但结果是undefiend

{
   "items": [
    {
      ..
    },
    {
      ..
    },
    {
      ..
    }
  ],..
  ,..
}

最佳答案

// 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]

关于javascript - 使用JavaScript从JSON获取数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29498053/

10-15 19:04