如何从JSON访问嵌套数组中的对象?

时间:2017-06-24 06:40:29

标签: arrays json node.js multidimensional-array

我试图从listitem数组中获取名称并将它们打印/存储在变量

这是结构:

{
 "items": [
  {
   "pagemap": {    
    "listitem": [
     {
      "name": "Transformers: The Last Knight"
     },
     {
      "name": "Transformers: The Last Knight"
     },
     {
      "name": "Transformers: The Last Knight"
     },
     {
      "name": "Despicable Me 3"
     },
     {
      "name": "Slumber Party"
     },
     {
      "name": "Everything, Everything"
     },
     {
      "name": "Can We Still Be Friends?"
     },
     {
      "name": "Transformers: The Last Knight"
     },
     {
      "name": "Wonder Woman"
     },
     {
      "name": "Transformers: The Last Knight"
     }
    ]
   }
  }

我还是node.js的新手,这就是我从JSON中提取它的方法

let{      
    items:[{
      pagemap: {
        listitem: [{              
        name                     
          }]
        }
    }]
} = showList;

如何获取listitem中的所有名称并将它们存储在变量中以便我可以打印它们?

3 个答案:

答案 0 :(得分:0)

let listItem = [];
showList.forEach((item) => {
    if(item && item.pagemap && item.pagemap.listitem){
       item.pagemap.listitem.forEach((lItem) => {
           if(lItem && lItem.name){
              listItem.push(lItem.name)
           }
       })
    }
})

答案 1 :(得分:0)

const results = {
 "items": [{
  "pagemap": {
   "listitem": [
    { "name": "Transformers: The Last Knight" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Despicable Me 3" },
    { "name": "Slumber Party" },
    { "name": "Everything, Everything" },
    { "name": "Can We Still Be Friends?" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Wonder Woman" },
    { "name": "Transformers: The Last Knight" }
   ]
  }
 }]
};

function nameOnly(listItem) {
    return listItem.name;
}
function resultItemsReducer(memo, item) {
    return memo.concat(item.pagemap.listitem.map(nameOnly));
}
const names = results.items.reduce(resultItemsReducer, []);
console.log(names);

Array.reduce()有详细记录on MDN以及Array.map()

每个功能都非常自我解释,但如果您需要更多详细信息,请告诉我。

答案 2 :(得分:0)

您可能需要考虑使用jsonpath。 libs有一个基于路径查询JSON的工具。安装和使用指南为here.

关于你的问题,通过使用jsonpath,你只能在你的JSON对象中访问“name”属性,如下所示

var jsonpath = require('jsonpath');
var data = {
 "items": [{
  "pagemap": {
   "listitem": [
    { "name": "Transformers: The Last Knight" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Despicable Me 3" },
    { "name": "Slumber Party" },
    { "name": "Everything, Everything" },
    { "name": "Can We Still Be Friends?" },
    { "name": "Transformers: The Last Knight" },
    { "name": "Wonder Woman" },
    { "name": "Transformers: The Last Knight" }
   ]
  }
 }]
};

// query only name from all listitem
var allnames = jsonpath.query(data, "$.items[0].pagemap.listitem[*].name")
console.log(allnames)

/* console.log on allnames returns - 
[ 'Transformers: The Last Knight',
  'Transformers: The Last Knight',
  'Transformers: The Last Knight',
  'Despicable Me 3',
  'Slumber Party',
  'Everything, Everything',
  'Can We Still Be Friends?',
  'Transformers: The Last Knight',
  'Wonder Woman',
  'Transformers: The Last Knight' ]
*/