2011-10-07 117 views
0

我有以下json,我面臨着爲此創建jsonobject的問題。請讓我知道如何創建嵌套級別的jsonobject。使用JSON嵌套級別

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

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