pihu
pihu

Reputation: 93

Using JSON for nested level

I have the following json ,i am facing issue with creating jsonobject for this.Please let me know how to create a jsonobject with nested levels.

{
    "menuConf": {
        "class": "menu horizontal dropdown",
        "caption": "",
        "id": "mainMenu",
        "container": "div",
        "contClass": "navigation main left",
        "helper": "span",
        "items": [
            {
                "caption": "a",
                "class": "orangesec",
                "link": "#",
                "id": "subMenu_1",
                "helper": "span",
                "items": [
                    {
                        "caption": "b",
                        "link": "#b"
                    },
                    {
                        "caption": "b",
                        "link": "#b"
                    },
                    {
                        "caption": "Blbogs",
                        "link": "#b"
                    },
                    {
                        "caption": "b",
                        "link": "#b"
                    }
                ]
            }
        ]
    }
}

Upvotes: 0

Views: 283

Answers (1)

thkala
thkala

Reputation: 86353

What's wrong with the JSONObject(String) constructor? Just store your JSON text in a string and use it - it should handle nested objects just fine:

String json = "{...}";

try {
    JSONObject o = new JSONObject(json);

    // Print out the JSON text with a 4-space indentation
    System.out.println(o.toString(4));
} catch (JSONException e) {
    e.printStackTrace();
}

Upvotes: 1

Related Questions