2016-06-09 60 views
0

我想瀏覽一個json對象,一次返回1個元素並遍歷。這是JSON。如何導航通過JSON對象

{ 
    "noun": { 
    "syn": [ 
     "leap", 
     "saltation", 
     "startle", 
     "start", 
     "parachuting", 
     "jumping", 
     "actuation", 
     "descent", 
     "inborn reflex", 
     "increase", 
     "innate reflex", 
     "instinctive reflex", 
     "physiological reaction", 
     "propulsion", 
     "reflex", 
     "transition", 
     "unconditioned reflex" 
    ] 
    }, 
    "verb": { 
    "syn": [ 
     "leap", 
     "bound", 
     "spring", 
     "startle", 
     "start", 
     "leap out", 
     "jump out", 
     "stand out", 
     "stick out", 
     "rise", 
     "climb up", 
     "jump off", 
     "derail", 
     "chute", 
     "parachute", 
     "jumpstart", 
     "jump-start", 
     "pass over", 
     "skip", 
     "skip over", 
     "alternate", 
     "alter", 
     "appear", 
     "assail", 
     "assault", 
     "attack", 
     "change", 
     "climb", 
     "dive", 
     "drop", 
     "enter", 
     "go", 
     "leave out", 
     "locomote", 
     "look", 
     "miss", 
     "mount", 
     "move", 
     "neglect", 
     "omit", 
     "overleap", 
     "overlook", 
     "participate", 
     "plunge", 
     "plunk", 
     "pretermit", 
     "seem", 
     "set on", 
     "shift", 
     "start up", 
     "switch", 
     "travel", 
     "vary", 
     "wax" 
    ], 
    "rel": [ 
     "leap out", 
     "jump on" 
    ] 
    } 
} 

比方說,我想訪問「飛躍」。這是兩層。我將如何1)返回跳躍,並且2)迭代到下一個單詞?

+0

假設你的JSON對象是'obj','obj.noun.syn [0]'應該做我應該提到的伎倆 – Lukman

+0

;我想將單詞存儲在變量中,並在遍歷數組時更新變量。 –

+0

看看這個http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Manish

回答

0

這是你在找什麼?

Object.keys(my_hash).forEach(function (key) { 
    Object.keys(my_hash[key]).forEach(function (key2) { 
     for(var i=0;i<my_hash[key][key2].length;i++) { 
      // Process here - You have access to all your words! 
      console.log(my_hash[key][key2][i]); 
     } 
    }) 
}) 
0

我以爲你想要做這樣的事情。

//Your array 
var array = {"noun":{"syn":["leap","saltation","startle","start","parachuting","jumping","actuation","descent","inborn reflex","increase","innate reflex","instinctive reflex","physiological reaction","propulsion","reflex","transition","unconditioned reflex"]},"verb":{"syn":["leap","bound","spring","startle","start","leap out","jump out","stand out","stick out","rise","climb up","jump off","derail","chute","parachute","jumpstart","jump-start","pass over","skip","skip over","alternate","alter","appear","assail","assault","attack","change","climb","dive","drop","enter","go","leave out","locomote","look","miss","mount","move","neglect","omit","overleap","overlook","participate","plunge","plunk","pretermit","seem","set on","shift","start up","switch","travel","vary","wax"],"rel":["leap out","jump on"]}}; 

//The list you want to iterate 
var words = array.noun.syn; 

//Iterating in the array 
    for (i=0; i<words.length; i++) { 
     //Words[i] is every value return after leap (including itself) 
     console.log(words[i]); 
    }