如何浏览JSON对象

时间:2016-06-09 04:01:02

标签: javascript json

我试图浏览一个json对象,一次返回1个元素并迭代。这是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)迭代到下一个词?

2 个答案:

答案 0 :(得分: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]);
        }
    })
})

答案 1 :(得分:0)

我以为你想做这样的事情。

SocketChannel