2

I have to make a tree like JSON structure with Java where I have a parent node with multiple children in it and so on. This is my code I have partially done this one but not completely successful to do it ..here is the output I need

{
    "name": "Culture", 
    "children": [
        {
            "name": "Salary"
        }, 
        {
            "name": "Work", 
            "children": [
                {
                    "name": "Effort"
                }, 
                {
                    "name": "trust"
                }
            ]
        }
    ]
}

but what I am generating is

{"name":"Culture",[{"name":"Salary"},{"name":"Work"},{"name":"Effort"}],"name":"Work",[{"name":"Culture"},{"name":"Work"}]}

Here is my code:

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

public class ParentChildApp {
public static class EntryListContainer {

    public List<Entry> children = new ArrayList<Entry>();
    public Entry name;

}

public static class Entry {

    private String name;

    public Entry(String name) {
        this.name = name;
    }

}

public static void main(String[] args) {

    EntryListContainer elc1 = new EntryListContainer();
    elc1.name = new Entry("Culture");
    elc1.children.add(new Entry("Salary"));
    elc1.children.add(new Entry("Work"));
    elc1.children.add(new Entry("Effort"));

    EntryListContainer elc2 = new EntryListContainer();
    elc2.name = new Entry("Work");
    elc2.children.add(new Entry("Culture"));
    elc2.children.add(new Entry("Work"));

    ArrayList<EntryListContainer> al = new ArrayList<EntryListContainer>();
    Gson g = new Gson();

    al.add(elc1);
    al.add(elc2);

    StringBuilder sb = new StringBuilder("{");
    for (EntryListContainer elc : al) {

        sb.append(g.toJson(elc.name).replace("{", "").replace("}", ""));
        sb.append(",");
        sb.append(g.toJson(elc.children));
        sb.append(",");
    }

    String partialJson = sb.toString();

    if (al.size() > 1) {
        int c = partialJson.lastIndexOf(",");
        partialJson = partialJson.substring(0, c);
    }

    String finalJson = partialJson + "}";
    System.out.println(finalJson);

    }

}
2
  • Why are you manipulating character strings to create JSON when you can just create the appropriate structure of Maps and Lists and have it created automatically?? Commented Nov 21, 2013 at 0:43
  • If you want to do with JSP, please check the below answer stackoverflow.com/a/56911382/3405508 Commented Jul 6, 2019 at 5:18

1 Answer 1

4

Do this:

package stackoverflow.questions;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.List;

public class ParentChildApp {

   public static class Entry {

      private String name;

      public Entry(String name) {
         this.name = name;
      }

      private List<Entry> children;

      public void add(Entry node){
         if (children == null)
            children = new ArrayList<Entry>();
         children.add(node);
      }

   }

   public static void main(String[] args) {

      Entry workNode = new Entry("Work");
      workNode.add(new Entry("Effort"));
      workNode.add(new Entry("Trust"));

      Entry salaryNode = new Entry("Salary");


      Entry cultureNode = new Entry("Culture");
      cultureNode.add(salaryNode);
      cultureNode.add(workNode);

      Gson g = new Gson();

      System.out.println(g.toJson(cultureNode));
   }

}

You will get exactly the JSON you are looking for.

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.