0

大家好,我正在使用 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?任何帮助将不胜感激!

4

3 回答 3

2

[] = 数组

{ } = 对象

你有一个数组,失去了环绕方括号。

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

于 2012-10-31T15:50:38.550 回答
1

您应该能够使用 for-in 循环:

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

这是你要找的吗?

于 2012-10-31T15:47:55.243 回答
1
for (var a in result[0].Answers) {
    result[0].Answers[a].AnswerID // Do something with it.
}
于 2012-10-31T15:48:09.737 回答