如何在嵌套的JSON结果中解析动态JSON密钥?我有以下格式的JSON结果,JSON Lint将其显示为“有效响应”。我的问题是:如何访问“question_mark”的内容,因为“141”,“8911”等都是动态值?我的示例代码用于访问“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"
},我的问题标题“动态密钥”可能是错误的,但我不知道在这一点上什么是这个问题的更好的名称。任何帮助将不胜感激!
3 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
使用JSONObject keys()获取密钥,然后迭代每个密钥以获取动态值。
代码大致如下:
// 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... }
添加回答
举报
0/150
提交
取消