0

Hi guys I am using parseJSON to parse this JSON string:

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);

but how do I get the responses/response ID's out of 'result' now? Any help would be greatly appreciated!

Joe
  • 457
  • 3
  • 8
  • 26

3 Answers3

2

[ ] = array

{ } = object

You have an array, lose the wrapping square brackets.

alert(json.Answers[0].AnswerText) = "Green"

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

You should be able to use the for-in loop:

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

Is this what you're looking for?

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58
1
for (var a in result[0].Answers) {
    result[0].Answers[a].AnswerID // Do something with it.
}
Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
  • I actually think 'a' would be an index within the result[0].Answers[] array. You would reference it like this: 'result[0].Answers[a]' – Ryan Griggs Oct 31 '12 at 16:00