Table of Contents
How to Access Dynamic JSON Keys in Nested JSON Results
Home Java javaTutorial How to Access Dynamic JSON Keys in Nested JSON Results?

How to Access Dynamic JSON Keys in Nested JSON Results?

Nov 06, 2024 pm 03:01 PM

How to Access Dynamic JSON Keys in Nested JSON Results?

How to Access Dynamic JSON Keys in Nested JSON Results

Problem:

You have a nested JSON result with dynamic keys, such as "141", "8911", etc. You need to access the content of these dynamic keys, such as the "count" or "more_description" values.

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 the content of dynamic JSON keys, follow these steps:

  1. Obtain the JSONObject representing the "question_mark" object using JSONObject.getJSONObject("question_mark").
  2. Use JSONObject.keys() to get an Iterator of the dynamic keys.
  3. Iterate through the keys using the Iterator.hasNext() and Iterator.next() methods.
  4. For each dynamic key, use JSONObject.getJSONObject(String key) to get the corresponding JSONObject representing the dynamic value.
  5. Access the desired values from the JSONObject representing the dynamic value.

Java Code:

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...
}
Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)