2017-10-15 24 views
0

我對json很新,我怎樣才能讓一個JSON對象的結構(輸出字符串)會是這樣?我正在使用org.json庫。這是一個json數組contians json數組嗎?如何製作JSON對象,輸出結果如下?

我輸入這樣的:

111(root) 
----222(child of 111) 
--------333(child of 222) 
--------444(child of 222) 
----123(child of 111) 
--------456(child of 123) 
--------456(child of 123) 

我怎樣才能讓一個JSON輸出會是什麼樣子的打擊,

{ 
"name": "flare", 
"children": [ 
    { 
     "name": "analytics", 
     "children": [ 
      { 
       "name": "cluster", 
       "children": [ 
        { 
         "name": "AgglomerativeCluster", 
         "value": 3938 
        }, 
        { 
         "name": "CommunityStructure", 
         "value": 3812 
        } 
       ] 
      }, 
      { 
       "name": "graph", 
       "children": [ 
        { 
         "name": "BetweennessCentrality", 
         "value": 3534 
        }, 
        { 
         "name": "LinkDistance", 
         "value": 5731 
        } 
       ] 
      } 
     ] 
    }, 
    { 
     "name": "animate", 
     "children": [ 
      { 
       "name": "Easing", 
       "value": 17010 
      }, 
      { 
       "name": "FunctionSequence", 
       "value": 5842 
      } 
     ] 
    } 
] 
} 

感謝您的幫助!

回答

1

你可以改變你的依賴和使用庫,允許對象映射,如傑克遜,或者你可以手工如下進行映射:

private static JSONObject toJSONObject(String name, Object value) { 
    JSONObject ret = new JSONObject(); 
    ret.put("name", name); 
    if (value != null) { 
     ret.put("value", value); 
    } 
    return ret; 
} 

public static JSONObject addChildren(JSONObject parent, JSONObject... children) { 
    parent.put("children", Arrays.asList(children)); 
    return parent; 
} 

public static void main(String[] sargs) { 
    JSONObject flare = toJSONObject("flare", null); 
    addChildren(flare, 
     addChildren(toJSONObject("analytics", null), 
      addChildren(toJSONObject("cluster", null), 
       toJSONObject("AgglomerativeCluster", 3938), 
       toJSONObject("CommunityStructure", 3812) 
      ), 
      addChildren(toJSONObject("graph", null), 
       toJSONObject("BetweennessCentrality", 3534), 
       toJSONObject("LinkDistance", 5731) 
      ) 
     ), 
     addChildren(toJSONObject("animate", null), 
      toJSONObject("Easing", 17010), 
      toJSONObject("FunctionSequence", 5842) 
     ) 
    ); 
    System.out.println(flare.toString()); 
} 
+0

非常感謝,但插入後如何檢索價值?我需要檢查一個孩子是否已經存在。像treemap。我嘗試過flare.getJSONObject(「children」),這會導致我錯誤「JSONObject [」children「]不是JSONObject。」 –

+0

非常感謝!我找到了一種方法來檢索JSONObject的值tempObject = tempJSONArray.getJSONObject(i)tempObject.get(「name」)。equals(name); –

0

你可以簡單地有這樣的課。

public class Node { 
    String name; 
    List<Node> children; 
    String value; 
    } 
+0

是的,我試了這個傑克遜lib,它扔我classnotfound異常。我添加了jackson-core-2.1.5.jar jackson-databind-2.1.5.jar,jackson-annotations-2.1.5.jar到path。 –

0

這可以通過ObjectMapper漂亮的印刷來實現。

public String pretty(Object object) throws JsonProcessingException { 
    return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object); 
} 

您可以使用我的圖書館。

<dependency> 
    <artifactId>json-utils</artifactId> 
    <groupId>org.bitbucket.swattu</groupId> 
    <version>1.0.16</version> 
</dependency> 

new JsonUtil().pretty(object);