Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><em>I looked at Google's Gson as a potential JSON plugin. Can anyone offer some form of guidance as to how I can generate Java from this JSON string?</em></p> </blockquote> <p><a href="http://code.google.com/p/google-gson/" rel="noreferrer">Google Gson</a> supports generics and nested beans. The <code>[]</code> in JSON represents an array and should map to a Java collection such as <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html" rel="noreferrer"><code>List</code></a> or just a plain Java array. The <code>{}</code> in JSON represents an object and should map to a Java <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Map.html" rel="noreferrer"><code>Map</code></a> or just some JavaBean class.</p> <p>You have a JSON object with several properties of which the <code>groups</code> property represents an array of nested objects of the very same type. This can be parsed with Gson the following way:</p> <pre><code>package com.stackoverflow.q1688099; 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&lt;Data&gt; groups; public String getTitle() { return title; } public Long getId() { return id; } public Boolean getChildren() { return children; } public List&lt;Data&gt; 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&lt;Data&gt; groups) { this.groups = groups; } public String toString() { return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups); } } </code></pre> <p>Fairly simple, isn't it? Just have a suitable JavaBean and call <a href="http://google-gson.googlecode.com/svn/tags/1.3/docs/javadocs/com/google/gson/Gson.html#fromJson%28java.lang.String,%20java.lang.Class%29" rel="noreferrer"><code>Gson#fromJson()</code></a>.</p> <h3>See also:</h3> <ul> <li><a href="http://json.org/" rel="noreferrer">Json.org</a> - Introduction to JSON</li> <li><a href="https://github.com/google/gson/blob/master/UserGuide.md" rel="noreferrer">Gson User Guide</a> - Introduction to Gson</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload