Newbie
Newbie

Reputation: 361

How to display an array returned by a json object (foreach dojo)

I have a json file returned as a json object (which is an array of arrays)...below is the returned json object

{
        "Info": {
        "Contact": "....  ",
        "title": "..."
            },
        "details": [
            {
                "ID": 1,
                "Question": "User ID",
                "Information": "",

            }, {
                "ID": 2,
                "Question": "Name",
                "Information": "",

            }, {
                "ID": 3,
                "Question": "Age",
                "Information": "",

            }
        ],
    "list": [
            {
                "No": 1,
                "response": ""
            }, {
                "No": 2,
                "response": ""
            }
        ]
}

Now i want to display only details...the below array

    "Details": [
        {
            "ID": 1,
            "Question": "User ID",
            "Information": "",

        }, {
            "ID": 2,
            "Question": "Name",
            "Information": "",

        }, {
            "ID": 3,
            "Question": "Age",
            "Information": "",

        }
    ],

How do i do this?? please help..

Thanks in advance.

Upvotes: 1

Views: 2538

Answers (2)

jbabey
jbabey

Reputation: 46657

1) parse the JSON into a javascript object

var parsedJSON = JSON.parse(jsonData);

2) access the properties you want

var details = parsedJSON.details;

edit: You are parsing your javascript object back into JSON, why??

working jsfiddle

Upvotes: 1

jessegavin
jessegavin

Reputation: 75690

var output = "";
for(var i=0; i<json.details.length; i++) {
  var detail = json.details[i];
  output += detail.ID +", "+ detail.Question +", "+ detail.Information +"\n";
}
alert(output);

Upvotes: 0

Related Questions