从JSON对象检索数据

时间:2014-07-30 21:57:40

标签: javascript json console

我对JSON很新,所以请不要烫伤我。我正试图找到一种方法从这些数据中检索信息,但我没有运气。

var services = [ "Hernia Repair", "Hip Replacement" ... ];

var places = {
  "place": [
      {
        "name": "The Whittington Hospital",        
        "service": [
          "Hernia Repair",
            {
              "ratings": [
                { "name": "John Doe", "score": 4, "review": "Some text here." },
                { "name": "Jane Doe", "score": 5, "review": "Some more text." }
              ]
            }
            ...
          ]
      },
  ]
};

我正在尝试获取service的名称,ratings的{​​{1}}和name,我一直在尝试将其输出到控制台像这样:

score

但这似乎不起作用。如果有人可以帮助解释我的嵌套是否正确,或者我是否以正确的方式检索它们。

对此有任何帮助表示赞赏。

P.S。是否还有一种方法可以优雅地将places.place[0].service.ratings.name places.place[0].service.ratings.score 数组中的“疝气修复”字符串链接到services对象中的“疝气修复”字符串?

1 个答案:

答案 0 :(得分:0)

服务是一个列表,评级是一个列表,所以这应该这样做:

places.place[0].service[1].ratings[0].name