0

JSON 示例

如何仅显示来自 JSON 的特殊信息?例如在 test.json 我们有

    {
    "productName": "Sencha Touch",
    "version": "2.0 Beta",
    "sampleData": [
        {
            "foo": "bar",
            "desc": "This sample content is loaded from the server."
        }
    ]
}

那么如何在按钮上单击 XMLHTTP。只显示来自“desc”的信息?

更新

如果我有这种如果JSON如何解析它?

[
    {
        "id": 7,
        "answers": [
            {
                "id": 6,
                "answer": "1",
                "isCorrect": false
            },
            {
                "id": 7,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 5,
                "answer": "3",
                "isCorrect": true
            }
        ],
        "question": "1+2=?"
    },
    {
        "id": 14,
        "answers": [
            {
                "id": 28,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 31,
                "answer": "7",
                "isCorrect": true
            },
            {
                "id": 29,
                "answer": "2",
                "isCorrect": false
            },
            {
                "id": 30,
                "answer": "6",
                "isCorrect": false
            }
        ],
        "question": "2+5=?"
    },
    {
        "id": 9,
        "answers": [
            {
                "id": 13,
                "answer": "3",
                "isCorrect": false
            },
            {
                "id": 11,
                "answer": "5",
                "isCorrect": false
            },
            {
                "id": 14,
                "answer": "6",
                "isCorrect": true
            },
            {
                "id": 12,
                "answer": "7",
                "isCorrect": false
            }
        ],
        "question": "3+3=?"
    }
]

谢谢!

4

1 回答 1

1

在的success回调中Ext.AjaxRequestresponse.responseText只是一个可以使用解码的字符串Ext.decode()。因此,解码该字符串,然后您可以将结果用作对象,并desc像使用任何 javascript 对象一样访问obj.sampleData[0].desc

因此,将该示例的第 141 行替换为以下行:

contentView.setHtml(Ext.decode(response.responseText).sampleData[0].desc);
于 2013-10-02T18:03:09.640 回答