如何显示json对象返回的数组(foreach dojo)

时间:2012-03-16 13:59:13

标签: javascript dojo

我有一个json文件作为json对象返回(这是一个数组数组)...下面是返回的json对象

{
        "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": ""
            }
        ]
}

现在我想只显示细节......下面的数组

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

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

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

        }
    ],

我该怎么做?请帮忙..

提前致谢。

2 个答案:

答案 0 :(得分:1)

1)将JSON解析为javascript对象

var parsedJSON = JSON.parse(jsonData);

2)访问您想要的属性

var details = parsedJSON.details;

编辑:你正在将你的javascript对象解析回JSON,为什么?

working jsfiddle

答案 1 :(得分:0)

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