如何使用javascript访问此JSON字符串中的响应/响应ID?

时间:2012-10-31 15:45:03

标签: javascript jquery

大家好我正在使用parseJSON来解析这个JSON字符串:

json = [
  {
    "Answers": [
      {
        "Responses": [

        ],
        "AnswerID": 1,
        "AnswerText": "Green"
      },
      {
        "Responses": [
          {
            "ResponseID": 1,
            "RespondingUser": null,
            "ResponseDate": "\/Date(1351694241577)\/"
          },
          {
            "ResponseID": 2,
            "RespondingUser": null,
            "ResponseDate": "\/Date(1351694245093)\/"
          }
        ],
        "AnswerID": 2,
        "AnswerText": "Blue"
      }
    ],
    "QuestionID": 1,
    "QuestionText": "Favourite colour?",
    "ClosingDate": "\/Date(1351953058527)\/",
    "AskingUser": null
  }
]

var result = jQuery.parseJSON(json);

但是如何从“结果”中获取响应/响应ID呢?任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

[] = array

{} = object

你有一个数组,丢失包装方括号。

alert(json.Answers [0] .AnswerText)=“绿色”

答案 1 :(得分:1)

您应该可以使用for-in循环:

 for (i in result[0].Answers)
 {
    // do something with result[0].Answers[i].Responses
 }

这是你要找的吗?

答案 2 :(得分:1)

for (var a in result[0].Answers) {
    result[0].Answers[a].AnswerID // Do something with it.
}