JavaScript JSON多级迭代解析

标签 javascript jquery json

{
   "id":0,
   "item":[
      {
         "id":"0-",
         "text":"BlueWing",
         "userdata":[
            {
               "name":"cid",
               "content":"10377"
            }
         ],
         "item":[
            {
               "id":"0-0",
               "text":"Auto",
               "userdata":[
                  {
                     "name":"cid",
                     "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533"
                  }
               ],
               "item":[
                  {
                     "id":"0-0-0",
                     "text":"Auto",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-0-0-0",
                           "text":"testaccount",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-0-0-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-0-0-0-1",
                                 "text":"testaccount",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-0-0-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-0-0-0-1-1",
                                       "text":"testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-0-0-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  },
                  {
                     "id":"0-0-1",
                     "text":"Balise Toyota",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10533"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-0-1-0",
                           "text":"Balise Toyota",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10533"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-0-1-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10533"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-0-1-0-1",
                                 "text":"Balise Toyota",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10533"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-0-1-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10533"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-0-1-0-1-1",
                                       "text":"Balise Toyota - 10533",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10533"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-0-1-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10533"
                                                }
                                             ]
                                          },
                                          {
                                             "id":"0-0-1-0-1-1-1",
                                             "text":"Balise.List - 11949",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":11949
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10533"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            },
            {
               "id":"0-1",
               "text":"BlueWing",
               "userdata":[
                  {
                     "name":"cid",
                     "content":"10667,10668"
                  }
               ],
               "item":[
                  {
                     "id":"0-1-0",
                     "text":"HVAC",
                     "userdata":[
                        {
                           "name":"cid",
                           "content":"10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302"
                        }
                     ],
                     "item":[
                        {
                           "id":"0-1-0-0",
                           "text":"HVAC",
                           "userdata":[
                              {
                                 "name":"cid",
                                 "content":"10667,10668"
                              }
                           ],
                           "item":[
                              {
                                 "id":"0-1-0-0-0",
                                 "text":"Add New List",
                                 "userdata":[
                                    {
                                       "name":"clid",
                                       "content":99999
                                    },
                                    {
                                       "name":"cid",
                                       "content":"10667,10668"
                                    }
                                 ]
                              },
                              {
                                 "id":"0-1-0-0-1",
                                 "text":"Mann Plumbing & Heating",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10667"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-1-0-0-1-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10667"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-1-0-0-1-1",
                                       "text":"Mann Plumbing & Heating - 10667",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10667"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-1-0-0-1-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10667"
                                                }
                                             ]
                                          },
                                          {
                                             "id":"0-1-0-0-1-1-1",
                                             "text":"Radius List - 12960",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":12960
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10667"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              },
                              {
                                 "id":"0-1-0-0-2",
                                 "text":"RES Air Conditioning",
                                 "userdata":[
                                    {
                                       "name":"cid",
                                       "content":"10668"
                                    }
                                 ],
                                 "item":[
                                    {
                                       "id":"0-1-0-0-2-0",
                                       "text":"Add New List",
                                       "userdata":[
                                          {
                                             "name":"clid",
                                             "content":99999
                                          },
                                          {
                                             "name":"cid",
                                             "content":"10668"
                                          }
                                       ]
                                    },
                                    {
                                       "id":"0-1-0-0-2-1",
                                       "text":"RES Air Conditioning - 10668",
                                       "userdata":[
                                          {
                                             "name":"cid",
                                             "content":"10668"
                                          }
                                       ],
                                       "item":[
                                          {
                                             "id":"0-1-0-0-2-1-0",
                                             "text":"Add New List",
                                             "userdata":[
                                                {
                                                   "name":"clid",
                                                   "content":99999
                                                },
                                                {
                                                   "name":"cid",
                                                   "content":"10668"
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

我需要遍历所有级别并按“id”进行匹配并获取“content”的值。

var id = "0-1-0-0-0";

如果 JSON 的 id 与 id 匹配,则返回 content 的值。 应返回 10667,10668

提前致谢!

最佳答案

基本上,您可以迭代对象中的所有元素,并在找到所需的 id 时停止迭代。

这个问题有什么问题,是第一级的半级,id1(级别0),1-(级别 1)和 1-0(级别 2)以及接下来的级别均用破折号分隔。

此提案使用 String#indexOf并检查所需索引为 0 的路径,并迭代该检查是否为 true。如果找到完整路径,则返回该对象。如果没有找到,则迭代和递归继续。

function getObject(object, path) {
    function getPart(o) {
        if (!path.indexOf(o.id)) {
            if (path === o.id) {
                result = o;
                return true;
            }
            return Array.isArray(o.item) && o.item.some(getPart);
        }
        return false;
    }

    var parts = path.split('-'),
        result;

    getPart(object);
    return result;
}

var object = { "id": 0, "item": [{ "id": "0-", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10377" }], "item": [{ "id": "0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533" }], "item": [{ "id": "0-0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1-1", "text": "testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }] }] }] }] }, { "id": "0-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1", "text": "Balise Toyota - 10533", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1-1", "text": "Balise.List - 11949", "userdata": [{ "name": "clid", "content": 11949 }, { "name": "cid", "content": "10533" }] }] }] }] }] }] }, { "id": "0-1", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302" }], "item": [{ "id": "0-1-0-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667,10668" }] }, { "id": "0-1-0-0-1", "text": "Mann Plumbing & Heating", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1", "text": "Mann Plumbing & Heating - 10667", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1-1", "text": "Radius List - 12960", "userdata": [{ "name": "clid", "content": 12960 }, { "name": "cid", "content": "10667" }] }] }] }, { "id": "0-1-0-0-2", "text": "RES Air Conditioning", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }, { "id": "0-1-0-0-2-1", "text": "RES Air Conditioning - 10668", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }] }] }] }] }] }] }] },
    id = "0-1-0-0-0",
    part = getObject(object, id);

document.write('<pre>' + JSON.stringify(part, 0, 4) + '</pre>');

关于JavaScript JSON多级迭代解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295425/

相关文章:

javascript - 如何将权限/授权数据传递给客户端 Javascript?

javascript - 单击禁用按钮时添加 CSS

jquery - 在 jQuery 插件内,返回 'this' 或 '$(this)' ?

javascript - 使用 Node 将大数组写入文件

javascript - 按内部文本 react native View 自动宽度

javascript - 多个 View 上的 Angular ng-model

javascript - HTML 在页面和 Flash 对象中同时滚动

javascript - 使用 PHP 从完整图像路径获取图像名称

json - 将嵌套接口(interface){}解码为用户提供的结构指针

php - 使用 Laravel 和 Guzzle 访问 JSON 中的数据数组