显示JSON中的选定信息

时间:2013-10-02 17:54:10

标签: sencha-touch-2 jsonp

Example with 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=?"
    }
]

THX!

1 个答案:

答案 0 :(得分:1)

success的{​​{1}}回调中,Ext.AjaxRequest只是一个可以使用response.responseText解码的字符串。因此,解码该字符串然后您可以将结果用作对象,并像使用任何javascript对象一样访问Ext.decode()desc

因此,用这一行替换该例子的第141行:

obj.sampleData[0].desc