Masalah:
Anda mempunyai hasil JSON bersarang dengan dinamik kekunci, seperti "141", "8911", dll. Anda perlu mengakses kandungan kunci dinamik ini, seperti nilai "count" atau "more_description".
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" }, { "product": "XYZ", "id": "1129", "question_mark": { "379": { "count": "379", "more_desc": "this is xyz", "seq": "5" }, "845": { "count": "845", "more_desc": "this is table", "seq": "6" }, "12383": { "count": "12383", "more_desc": "Jumbo", "seq": "4" }, "257258": { "count": "257258", "more_desc": "large", "seq": "1" } }, "name": "some other name", "description": "this is some other product" } ] }
Penyelesaian:
Untuk mengakses kandungan kekunci JSON dinamik, ikut langkah berikut:
Kod Java:
JSONObject questionMark = searchResult.getJSONObject("question_mark"); Iterator keys = questionMark.keys(); while(keys.hasNext()) { String currentDynamicKey = (String)keys.next(); JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey); // Access and manipulate the content of the currentDynamicValue... }
Atas ialah kandungan terperinci Bagaimana untuk Mengakses Kekunci JSON Dinamik dalam Keputusan JSON Bersarang?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!