如何在嵌套的JSON結(jié)果中解析動(dòng)態(tài)JSON密鑰?我有以下格式的JSON結(jié)果,JSON Lint將其顯示為“有效響應(yīng)”。我的問題是:如何訪問“question_mark”的內(nèi)容,因?yàn)椤?41”,“8911”等都是動(dòng)態(tài)值?我的示例代碼用于訪問“product”的值。//Consider I have the first <code>JSONObject</code> of the "search_result" array and //I access it's "product" value as below.String product = jsonObject.optString("product"); //where jsonObject is of type JSONObject.//<code>product<code> now contains "abc".JSON:{
"status": "OK",
"search_result": [
{
"product": "abc",
"id": "1132",
"question_mark": {
"141": {
"count": "141",
"more_description": "this is abc",
"seq": "2"
},
"8911": {
"count": "8911",
"more_desc": "this is cup",
"seq": "1"
}
},
"name": "some name",
"description": "This is some product"
},我的問題標(biāo)題“動(dòng)態(tài)密鑰”可能是錯(cuò)誤的,但我不知道在這一點(diǎn)上什么是這個(gè)問題的更好的名稱。任何幫助將不勝感激!
3 回答

白衣非少年
TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
使用JSONObject keys()獲取密鑰,然后迭代每個(gè)密鑰以獲取動(dòng)態(tài)值。
代碼大致如下:
// searchResult refers to the current element in the array "search_result" JSONObject questionMark = searchResult.getJSONObject("question_mark"); Iterator keys = questionMark.keys(); while(keys.hasNext()) { // loop to get the dynamic key String currentDynamicKey = (String)keys.next(); // get the value of the dynamic key JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey); // do something here with the value... }
添加回答
舉報(bào)
0/150
提交
取消