2013-10-27 64 views
-1

我必須製作一個Json,它將形成一個像結構樹一樣的結構。我已經寫了一個Java代碼,它正在形成一個像這樣的結構,但它不符合目的。在那裏我已經這樣做了far..This是我的java類..如何製作一個hirarchical樹結構Json使用java程序

import com.google.gson.FieldNamingStrategy; 
import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import java.lang.reflect.Field; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class GsonProgram { 

public static void main(String... args) throws Exception { 
    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(); 
     Entry entry2 = new Entry(); 
     entry1.setChildren(arr[i]); 
     entryList1.add(entry1); 
     entryList2.add(entry2); 

    entryListContainer.setEntryList1(entryList1); 
    entryListContainer.setEntryList1(entryList2); 

    /*Root node this will collapse and get back to Original position on click*/ 
    entryListContainer.setName("Employee"); 

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

} 類DynamicFieldNamingStrategy實現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> entryList1; 

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

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

class Entry { 

private String name; 

public String getChildren() { 
    return name; 
} 

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

,這是生成的JSON

{ 
"name":[{ 
    "name":"Salary" 
},{ 
    "name":"Salary" 
},{ 
    "name":"Work" 
},{ 
    "name":"Doller" 
}] 
} 

但我想這種結構......

{ 
"name":"Employee", 
"children":[{ 
    "name":"Salary" 
},{ 
    "name":"Salary" 
},{ 
    "name":"Work" 
},{ 
    "name":"Doller" 
}] 
} 

我需要json.somebody請幫助這個格式....

回答

0

僞類結構應該是:

class DynamicFieldNamingStrategy{ 
    private String name; 
    private List<Entry> children; 

} 

GsonProgram

public class GsonProgram { 

    public static void main(String... args) throws Exception { 
     Entry entry1 = new Entry(); 
     entry1.setChildren("Salary"); 
     Entry entry2 = new Entry(); 
     entry2.setChildren("Salary"); 
     Entry entry3 = new Entry(); 
     entry3.setChildren("Work"); 
     Entry entry4 = new Entry(); 
     entry4.setChildren("Doller"); 
     EntryListContainer entryListContainer = new EntryListContainer(); 
     ArrayList<Entry> entryList1 = new ArrayList<Entry>(); 


     entryList1.add(entry1); 
     entryList1.add(entry2); 
     entryList1.add(entry3); 
     entryList1.add(entry4); 

     entryListContainer.setEntryList1(entryList1); 
     entryListContainer.setName("Employee"); 

     Map<String, String> mapping = new HashMap<String, String>(); 
     mapping.put("entryList1", "name"); 



     Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping)).create(); 
     System.out.println(gson.toJson(entryListContainer)); 
    } 
} 

EntryListContainer

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; 
    } 
} 

輸出:

{ 
    "children": [ 
     { 
      "name": "Salary" 
     }, 
     { 
      "name": "Salary" 
     }, 
     { 
      "name": "Work" 
     }, 
     { 
      "name": "Doller" 
     } 
    ], 
    "name": "Employee" 
} 
+0

非常感謝馬克西姆Shoustin – lucifer

+0

@Maxmim你能幫助我與你的代碼,我應該怎麼做才能使兒童的子樹,即我想從一個樹從doller那裏也將有三個節點1>薪水2>工作3>責任 – lucifer

+0

我這樣做,但最後添加entryListContainer名稱即經理即將come.will你幫我??? – lucifer