如何迭代以下JSON对象?

时间:2016-06-03 10:12:19

标签: javascript json iteration

我有以下 public void processEvents() throws IOException, InterruptedException { for (; ; ) { WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } Path dir = keys.get(key); if (dir == null) { logger.error("WatchKey not recognized!!"); continue; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == OVERFLOW) { continue; } // Context for directory entry event is the file name of entry WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); // System.out.format("%s: %s\n", event.kind().name(), child); syncer.sync(child); // if directory is created, and watching recursively, then // register it and its sub-directories if (recursive && (kind == ENTRY_CREATE)) { try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException x) { // ignore to keep sample readbale } } } 结构:

JSON

现在,如果我必须找到"123456789": { "id": "Some_Id", "code": "Some_Code", "name": "Some_Name", }, "123456789": { "id": "Some_Id", "code": "Some_Code", "name": "Some_Name", },, "123456789": { "id": "Some_Id", "code": "Some_Code", "name": "Some_Name", } } 内的每个对象的名称,我该如何迭代?

3 个答案:

答案 0 :(得分:-1)

Object.keys(theObject).forEach(function (key) {
  var item = theObject[key];
  console.log(item.name);
});

答案 1 :(得分:-1)

您可以使用简单的jQuery

arr = $.parseJSON(arr);
$.each(msg, function(i, key){
alert(key.id);
});

这只是迭代

的一个例子

答案 2 :(得分:-1)

  

首先,一个对象不能有多个相同名称的键。

使用Object.keys获取对象的键并使用Array#foreach

迭代它们

var input = {
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  },
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  },
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  }
};
console.log(Object.keys(input)) //Only one key
Object.keys(input).forEach(function(item) {
  console.log(input[item].name);
});