2017-10-15 3 views
0

私はjsonにとって非常に新しいです、どうすればJSONオブジェクトを構造体(出力文字列)にすることができますか?私はorg.jsonライブラリを使用しています。JSONオブジェクトを作成するにはどうすればいいですか?出力は次のようになりますか?

これはjson配列のcontians 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) 

にはどうすれば出力はあなたのため

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

おかげで助けて、ブローのようになるJSONを作ることができます!

答えて

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

はい、私はjackson libでこれを試しましたが、classnotfoundの例外がスローされます。私は、jackson-core-2.1.5.jar jackson-databind-2.1.5.jar、jackson-annotations-2.1.5.jarをパスに追加しました。 –

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); 
関連する問題