Home > Java > javaTutorial > How to Access Dynamic JSON Keys in Nested Results?

How to Access Dynamic JSON Keys in Nested Results?

Linda Hamilton
Release: 2024-11-10 15:26:03
Original
1021 people have browsed it

How to Access Dynamic JSON Keys in Nested Results?

Accessing Dynamic JSON Keys in Nested Results

In JSON data, keys can be dynamic, meaning they vary from object to object. This can pose a challenge when accessing specific values, especially in nested structures.

To handle dynamic JSON keys in a nested result, you can leverage the keys() method in the JSONObject class. This method returns an iterator that allows you to iterate over the available keys in the JSON object.

Consider the following JSON structure:

{
 "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"
            }
       ]
}
Copy after login

To access the content of the "question_mark" field for each object in the "search_result" array, you can use the following code:

for (JSONObject searchResult : searchResults) {
    JSONObject questionMark = searchResult.getJSONObject("question_mark");
    Iterator<String> keys = questionMark.keys();
    
    while (keys.hasNext()) {
        String currentDynamicKey = keys.next();
        
        // Get the value of the dynamic key
        JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);
        
        // Do something here with the value...
    }
}
Copy after login

This code iterates over the available keys in the "question_mark" field and retrieves the value associated with each key. You can then use these values for further processing or display.

The above is the detailed content of How to Access Dynamic JSON Keys in Nested Results?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template