1

How should I create algorithm on creation of following JSON pattern using Java ?

Target JSON pattern is as follows

      {
"title":"ABC",
"children":[{
    "title":"ABC_SUK",
    "children":[{
        "title":"ABC_SUK_Block_01",
        "children":[{
            "title":"XXX_201",
        },{
            "title":"XXX_202",  
        }]
    },{
        "title":"ABC_SUK_Block_02",
        "children":[{
            "title":"XXX_203",
        },{
            "title":"XXX_204",  
        }]
    }]
     }]
 }

From following table

            group | site   | block           |hostname
           _________________________________________
           ABC   | ABC_suk | ABC_SUK_Block_01|XXX_201
           ABC   | ABC_suk | ABC_SUK_Block_01|XXX_202
           ABC   | ABC_suk | ABC_SUK_Block_02|XXX_203
           ABC   | ABC_suk | ABC_SUK_Block_02|XXX_204

I am adding 1 object 1 row to ArrayList<Object>

3
  • Do you mean ArrayList -> JSon? Commented Jul 1, 2013 at 7:41
  • parse it using Jackson or Gson ...... basically the class structure you create for parsing should be done in a way that it takes grouping into account.... Commented Jul 1, 2013 at 7:47
  • Yes I mean. Can you guide me. Thank you(johnchen902) Commented Jul 1, 2013 at 7:50

1 Answer 1

1

You can create one or more java classes with the structure described above and than you can use a java to json parser (I strongly recommend Jackson) to transform in json the ojbect. For example:

class MyClass {
   String title;
   List<MyClass> children;
   //getters and setters
   //
}

You need to load the data from the DB to the class and than use jackson. Read a tutorial, it's very simple: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.