2016-03-29 17 views
1
{ 
    "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"の値を取得する必要があります。JavaScript JSONマルチレベル反復構文解析

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

JSONのidは、IDと一致した場合、その後contentの値を返します。 返信する必要があります10667,10668

ありがとうございます!

+2

、あなたは何を試してみましたか? –

+0

多くのことを試してみましたが、必要な出力に近くなるものはありません。私が得た最も近いものは 'console.log(getObjects(json、 'id'、" 0-1-0-0-0 "));ですが、返すコンテンツを取得することはできません。 – Kim

答えて

1

基本的に、オブジェクト内のすべての要素を反復処理し、必要な場合は反復を停止することができます。idこの問題は、それほど問題になりいただきまし

は、id1等(レベル0)、1-(レベル1)と1-0(レベル2)、次のレベルですべてのダッシュで区切られた、最初のレベルの半分のレベルです。

この提案では、String#indexOfを使用して、希望のインデックスが0のパスをチェックし、そのチェックが真であれば繰り返しを繰り返します。完全なパスが見つかった場合は、オブジェクトを返します。見つからなければ反復と再帰が続きます。

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>');

+1

ブリリアント!魅力のように働いた。ありがとうございました。 – Kim