从JSON获取键值对,而不使用Java脚本或jquery中的。(DOT)

时间:2015-03-13 23:16:37

标签: javascript jquery json

如何在不使用点(.)的情况下从JSON下面获取数据?

var JsonObj = {
  "database.status" : {
    "healthy" : false,
    "error" : {
      "message" : null,
      "stack" : [ "com.dhrubajyoti.tvs.utilities.DatabaseHealthCheck.check(DatabaseHealthCheck.java:26)", "com.codahale.metrics.health.HealthCheck.execute(HealthCheck.java:172)" ]
    }
  },
  "gs.status" : {
    "healthy" : true
  },
  "last.incremental.import" : {
    "healthy" : false,
    "error" : {
      "message" : null,
      "stack" : [ "org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator.createPreparedStatement(JdbcTemplate.java:1436)", "org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:581)", "org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)", "org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666)", "org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674)", "org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:734)", "com.dhrubajyoti.tvs.dao.IncrementalImportLogDaoImpl.findLatestImport(IncrementalImportLogDaoImpl.java:76)"]
    }
  },
  "last.incremental.update" : {
    "healthy" : false,
    "error" : {
      "message" : null,
      "stack" : [ "org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator.createPreparedStatement(JdbcTemplate.java:1436)" ]
    }
  },
  "memory.status" : {
    "healthy" : false,
    "message" : "Used memory 3 GB. Current threshold 0.8315874 is above alarm threshold 0.8"
  }
}

2 个答案:

答案 0 :(得分:2)

使用[]表示法,如下所示。阅读here for more info.

for (key in JsonObj) {
    alert(key + " is healthy: " + JsonObj[key]["healthy"]);
}

答案 1 :(得分:0)

不太确定你在问什么。您是否在询问如何使用括号表示法将键指定为字符串而不是点属性? JavaScript object: access variable property by name as string