-1

I have pasted a sample JSON Structure which I am trying to achieve using java program finally the output should be in JSONObject. I tried using map but it became very complex if I was not going wrong.

{
"name": "flare",
"children": [
    {
    "name": "analytics",
    "children": [
        {
        "name": "cluster",
        "children": [
            {"name": "AgglomerativeCluster"},
            {"name": "CommunityStructure"},
            {"name": "HierarchicalCluster"},
            {"name": "MergeEdge"}
        ]
        }]
    },
    {
    "name": "graph",
    "children": [
        {"name": "BetweennessCentrality"},
        {"name": "LinkDistance"},
        {"name": "MaxFlowMinCut"},
        {"name": "ShortestPaths"},
        {"name": "SpanningTree"}
    ]
    }
]
}
alok verma
  • 47
  • 5

3 Answers3

0

You can try using GSON library by google. You can create Java objects and GSON turns them into JSON automatically. Have a look: https://code.google.com/p/google-gson/ The disadvantage of using JSONObject directly would be that it would become very complicated in your case. And as the data you want to put in JSON grows, it would become very difficult.

Pranava Sheoran
  • 529
  • 4
  • 27
0

Use JSONObject

For example:

myString = new JSONObject().put("JSON", "Hello, World!").toString();

produces the string {"JSON": "Hello, World"}.

stepanian
  • 11,373
  • 8
  • 43
  • 63
0

Using GSON library is easy. Create Person class with an attribute to an array of same class.

class Person {
    private String name;
    private Person[] children;

    //getters

    //setters
}

and then, use it to get instance from json

// Create person
Person person = new Gson().fromJson(jsonString, Person.class);
// Get children
Person[] childrens = person.getChildren();
jgonza73
  • 344
  • 1
  • 10