从JSON数据中获取价值

时间:2017-09-13 12:35:58

标签: javascript json node.js

我有这个JSON数据,我希望得到每个孩子的slug值。

主要问题是我不知道每次获得新数据后会生成多少孩子。总之,孩子内部的孩子是动态的,不固定。

那么,如何从JSON数据中出现的每个子节点获取slug的值?

这里我写了一个JSON数据:

[
    {
        "id": 11,
        "title": "Bottle",
        "__domenu_params": {},
        "href": "www.products.com",
        "target": "_blank",
        "slug": "/undefined"
    },
    {
        "id": 10,
        "title": "Pencils",
        "__domenu_params": {},
        "slug": "/Pencils"
    },
    {
        "id": 9,
        "title": "Stationary",
        "__domenu_params": {},
        "slug": "/Stationary"
    },
    {
        "id": 8,
        "title": "Pen",
        "__domenu_params": {},
        "slug": "/Pen"
    },
    {
        "id": 7,
        "title": "Cable",
        "__domenu_params": {},
        "slug": "/Cable"
    },
    {
        "id": 5,
        "title": "Electronics",
        "__domenu_params": {},
        "slug": "/Electronics",
        "children": [
            {
                "id": 4,
                "title": "Charger",
                "__domenu_params": {},
                "slug": "/Charger"
            },
            {
                "id": 3,
                "title": "Laptop",
                "__domenu_params": {},
                "slug": "/Laptop"
            },
            {
                "id": 2,
                "title": "Mobile",
                "__domenu_params": {},
                "slug": "/Mobile",
                "href": "www.electronics.com",
                "target": "_blank",
                "children": [
                    {
                        "id": 6,
                        "title": "Pendrive",
                        "__domenu_params": {},
                        "slug": "/Pendrive",
                        "href": "www.pendrive.com",
                        "target": "_self"
                    }
                ]
            }
        ]
    }
]

我尝试了这段代码,只从这个JSON中获取了slug的值。我该怎么做以获取每个可能的JSON数据?

let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}]


for (let i = 0; i< data.length ; i++) {
    // console.log(data[i]["children"])
    if (data[i]["children"]) {
        console.log("inside")
        for (let j = 0; j < data[i]["children"].length; j++) {
            // console.log(data[i]["children"][j])
            if (data[i]["children"][j]["children"]) {
                console.log(data[i]["children"][j]["children"])
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Recursion轻松解析此数据:

let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}]


function getAllSlugs(categories) {
  // For each category...
  return categories.reduce((slugList, category) => {
    // add the slug for the particular category...
    slugList.push(category.slug);
    // and, check if the category has children...
    if (category.children && category.children.length) {
      // if children are there, call the same function with the
      // children and add the slugs of children
      slugList = slugList.concat(getAllSlugs(category.children));
    }
    return slugList;
  }, []);
}
// Call the function
getAllSlugs(data);

输出:

[ '/undefined',
  '/Pencils',
  '/Stationary',
  '/Pen',
  '/Cable',
  '/Electronics',
  '/Charger',
  '/Laptop',
  '/Mobile',
  '/Pendrive' ]

希望这有帮助!