【问题标题】:Get value from JSON data [closed]从 JSON 数据中获取价值 [关闭]
【发布时间】:2017-09-13 21:25:17
【问题描述】:

我有这个 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"])
            }
        }
    }
}

【问题讨论】:

  • 我们不鼓励发表断章取义的帖子,并希望社区能够解决它。假设您尝试自己解决并陷入困境,如果您写下您的想法和您无法弄清楚的内容,这可能会有所帮助。它肯定会为您的帖子吸引更多答案。在此之前,该问题将被投票关闭/否决。
  • @Cerbrus 现在好吗?
  • 检查对象是否包含children数组。如果是,请使用递归再次将该子数组解析为数据。
  • @tbking 在 child 中有一个 child,最多 n 次。那么,我该如何计算呢?
  • @HarshPatel 我提交了一个答案,展示了如何使用递归来解析数据。希望它能解决您的疑问。

标签: javascript json node.js


【解决方案1】:

您可以使用递归轻松解析这些数据:

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多