JavaScript JSON多级迭代解析

时间:2016-03-29 21:28:31

标签: 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"进行匹配并获得"内容"。

的价值
var id = "0-1-0-0-0";

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

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

这个问题是如此成问题,是第一级的半级,id1(级别0),1-(级别1)和{{1} }(级别2)和下一级别用破折号分隔。

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



0