Revision 38ea8131-a713-4a53-88ee-0ef35e5e3625 - Stack Overflow

We have chosen [Gson][1] because it has the best support for **Generics** and **nested beans** and it is very **performant**. Your example can be solved the following way:

 package test;
 
 import java.util.List;
 
 import com.google.gson.Gson;
 
 public class Test {
 
 public static void main(String... args) throws Exception {
 String json = 
 "{"
 + "'title': 'Computing and Information systems',"
 + "'id' : 1,"
 + "'children' : 'true',"
 + "'groups' : [{"
 + "'title' : 'Level one CIS',"
 + "'id' : 2,"
 + "'children' : 'true',"
 + "'groups' : [{"
 + "'title' : 'Intro To Computing and Internet',"
 + "'id' : 3,"
 + "'children': 'false',"
 + "'groups':[]"
 + "}]" 
 + "}]"
 + "}";

 // Now do the magic.
 Data data = new Gson().fromJson(json, Data.class);

 // Show it.
 System.out.println(data);
 }
 
 }
 
 class Data {
 private String title;
 private Long id;
 private Boolean children;
 private List<Data> groups;
 
 public String getTitle() { return title; }
 public Long getId() { return id; }
 public Boolean getChildren() { return children; }
 public List<Data> getGroups() { return groups; }
 
 public void setTitle(String title) { this.title = title; }
 public void setId(Long id) { this.id = id; }
 public void setChildren(Boolean children) { this.children = children; }
 public void setGroups(List<Data> groups) { this.groups = groups; }
 
 public String toString() {
 return "title:" + title + ",id:" + id + ",children:" + children + ",groups:" + groups;
 }
 }

Fairly simple, isn't it? Just have a suitable Javabean and call `Gson#fromJson()`.

 [1]: http://code.google.com/p/google-gson/

AltStyle によって変換されたページ (->オリジナル) /