将JSON用于嵌套级别

时间:2011-10-07 10:31:03

标签: java json

我有以下json,我面临着为此创建jsonobject的问题。请让我知道如何使用嵌套级别创建一个jsonobject。

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

1 个答案:

答案 0 :(得分:1)

JSONObject(String)构造函数有什么问题?只需将您的JSON文本存储在一个字符串中并使用它 - 它应该处理嵌套对象:

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();
}