从JSON数据中获取价值

问题描述:

我有这个JSON数据与我,我想从每个孩子得到slu value的价值。从JSON数据中获取价值

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

那么,如何从JSON数据中存在的每个孩子获得slu value的值?

在这里,我写一个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得到塞的价值。我应该怎么做才能获得所有可能的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"]) 
      } 
     } 
    } 
} 
+2

我们不鼓励那些仅仅从背景中提出问题的帖子,并期望社群解决它。假设你试图自己解决它并陷入困境,那么如果你写下了你的想法和你无法想象的东西,这可能会有所帮助。它肯定会为你的帖子提供更多的答案。在此之前,这个问题将被投票停止/降低投票。 – Cerbrus

+0

@Cerbrus现在好吗? –

+0

检查对象是否包含'children'数组。如果是这样,则使用递归来再次将该子数组解析为数据。 – tbking

您可以使用递归来轻松地分析这些数据:

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

希望这有助于!