#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    How to create Multilevel Hirarchy in Json


    ..I have to make a tree like json structure.which will represent a phone network. i.e a_no may call to a set of b_no s and from the set of b_no s there may also exist a network.So the entire network consist of a hierarchical tree structure of dynamic json elements.I have done the first level of representation.But I also need a full network. Here is the code I have done...

    class Entry {

    private String name;

    public String getChildren() {
    return name;
    }

    public void setChildren(String name) {
    this.name = name;
    }
    }

    public class JsonApplication {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    String arr[] = {"Culture", "Salary", "Work", "Effort"};
    EntryListContainer entryListContainer = new EntryListContainer();
    List<Entry> entryList1 = new ArrayList<>();

    for (int i = 0; i < arr.length; i++) {
    Entry entry1 = new Entry();
    entry1.setChildren(arr[i]);
    entryList1.add(entry1);
    /*Child nodes are created here and put into entryListContainer*/
    entryListContainer.setEntryList1(entryList1);
    /*Root node this will collapse and get back to Original position on click*/
    entryListContainer.setName("Employee");
    }


    Map<String, String> mapping = new HashMap<>();
    mapping.put("entryList1", "name");
    Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping)).create();
    System.out.println(gson.toJson(entryListContainer));
    }
    }

    class DynamicFieldNamingStrategy implements FieldNamingStrategy {

    private Map<String, String> mapping;

    public DynamicFieldNamingStrategy(Map<String, String> mapping) {
    this.mapping = mapping;
    }

    @Override
    public String translateName(Field field) {
    String newName = mapping.get(field.getName());
    if (newName != null) {
    return newName;
    }

    return field.getName();
    }
    }

    class EntryListContainer {

    private List<Entry> children;
    private String name;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void setEntryList1(List<Entry> entryList1) {
    this.children = entryList1;
    }

    public List<Entry> getEntryList1() {
    return children;
    }
    }


    In this code the json I am getting resulting a root node with some child node.But I also need multilevel hierarchy i.e a child node will also be a root node and it may have also a couple of children , i.e work is a child node I want to make it a parent node after Employee , and it also will have 3 children Salary,Effort,Dedication.I can not do this.My code is generating a root node with a couple of child nodes.But I want a multilevel hierarchy
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Can you post an example of how you want the JSON structure to look like?

IMN logo majestic logo threadwatch logo seochat tools logo