将JSON数据提取到Javascript对象数组中

时间:2016-09-12 10:23:54

标签: javascript jquery json

以下是我的JSON数据:

[
    {
        "items": [
            {
                "retailername": "Zop Now",
                "value": 475
            },
            {
                "retailername": "Snap Deal",
                "value": 265
            },
            {
                "retailername": "Natures Basket",
                "value": 86
            }
        ]
    }
]

我想要的是:

[
    {
        "retailername": "Zop Now",
        "value": 475
    },
    {
        "retailername": "Snap Deal",
        "value": 265
    },
    {
        "retailername": "Natures Basket",
        "value": 86
    }
]

很大一部分来自上述数据。

提前致谢。

5 个答案:

答案 0 :(得分:1)

检查一下:



var data = [{"items":[{"retailername":"Zop Now","value":475},{"retailername":"Snap Deal","value":265},{"retailername":"Natures Basket","value":86}]}]

var items = data[0]["items"];

console.log(items);




答案 1 :(得分:1)

这样的事情应该符合你的期望:

var jsonbase = [{
  "items": [{
    "retailername": "Zop Now",
    "value": 475
  }, {
    "retailername": "Snap Deal",
    "value": 265
  }, {
    "retailername": "Natures Basket",
    "value": 86
  }]
}];



var json = jsonbase[0].items;

console.log(json);

https://jsfiddle.net/Tintin37/scuozurn/

答案 2 :(得分:0)

请尝试以下操作:

let data, arr;
    data = [{"items":[{"retailername":"Zop Now","value":475},{"retailername":"Snap Deal","value":265},{"retailername":"Natures Basket","value":86}]}];
    arr = [];
    for(let i =0; i < data.length; i++){
        if(data[i].hasOwnProperty('items') && data[i].items instanceof Array){
            arr.push(data[i].items);
        }
    }
    console.log(arr);

答案 3 :(得分:0)

&#13;
&#13;
var obj = [{
  "items": [{
    "retailername": "Zop Now",
    "value": 475
  }, {
    "retailername": "Snap Deal",
    "value": 265
  }, {
    "retailername": "Natures Basket",
    "value": 86
  }]
}];



var getJson = obj[0]['items']; //or

var get_Json = obj[0].items;

console.log(getJson) // or ;
   console.log(get_Json);
&#13;
&#13;
&#13;

  

https://jsfiddle.net/36e91rr4/1/

答案 4 :(得分:0)

如果您仍然遇到错误,可能您没有有效的javascript对象,并且它可能是字符串格式。

var obj = JSON.parse(your_original_object);

然后,

var get_Json = obj[0].items;

应该为您提供您正在寻找的对象。