2013-10-02 118 views
0

Example with JSON顯示來自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」的信息?

UPDATE

,如果我有這樣那樣,如果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

success回調Ext.AjaxRequest,response.responseText只是一個字符串,可以使用Ext.decode()解碼。因此,解碼該字符串,然後您可以將結果用作對象,並像訪問任何javascript對象一樣訪問descobj.sampleData[0].desc

所以,用這條線替換實例進行線上141:

contentView.setHtml(Ext.decode(response.responseText).sampleData[0].desc); 
+0

是的,謝謝。但有一點問題。如果我沒有其他JSON,請參閱更新。有什麼想法? – user2483213

+0

與其他任何javascript對象一樣... – kevhender

+0

我嘗試'contentView.setHtml(Ext.decode(response.responseText).question [0]);',但得到了錯誤 – user2483213