简体   繁体   English

如何浏览JSON对象

[英]How To Navigate Through JSON Object

I am trying to navigate through a json object to return 1 element at a time and iterate through. 我正在尝试浏览json对象,一次返回1个元素并进行迭代。 Here's the 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"
    ]
  }
}

Let's say I wanted to access "leap." 假设我想访问“飞跃”。 It's two layers in. How would I 1) return leap, and 2) iterate to the next word? 它是两层的。我将如何1)返回跳跃,2)迭代到下一个单词?

Is this what you are looking for? 这是你想要的?

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]);
        }
    })
})

I thought you wanna do something like this. 我以为你想做这样的事情。

//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]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM