且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解析嵌套 JSON 结果中的动态 JSON 键?

更新时间:2023-02-14 12:02:31

使用 JSONObject keys() 获取键,然后迭代每个键以获取动态值.

Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.

代码大致如下:


// searchResult refers to the current element in the array "search_result" but whats searchResult?
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...
}