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

How to Access Dynamic Keys in Nested JSON Results?

DDD
Release: 2024-11-08 00:54:03
Original
974 people have browsed it

How to Access Dynamic Keys in Nested JSON Results?

Parsing Dynamic JSON Keys in Nested JSON Results

Question:

How to access the content of keys that are dynamic values within a JSON result?

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

Solution:

To access dynamic keys in a nested JSON result, use the keys() method of the JSONObject class. This method returns an Iterator that can be used to iterate over the keys in the object. Once you have the keys, you can use the getJSONObject(key) method to retrieve the corresponding values.

Here is an example of how to parse the dynamic keys in the example JSON:

JSONObject searchResult = ...;  // Assuming searchResult is a JSONObject

JSONObject questionMark = searchResult.getJSONObject("question_mark");
Iterator keys = questionMark.keys();

while (keys.hasNext()) {
    String currentDynamicKey = (String) keys.next();
    JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);

    // Do something with the dynamic key and value...
}
Copy after login

This code iterates over the keys in the question_mark object and retrieves the corresponding values. You can then use these values to access the data you need.

The above is the detailed content of How to Access Dynamic Keys in Nested JSON 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template