Created
September 7, 2011 16:14
-
-
Save rsaunders100/1200999 to your computer and use it in GitHub Desktop.
(Android) Example use of the excelent Gson library used to parse JSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .jar from: | |
// http://code.google.com/p/google-gson/ | |
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 String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment