Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the easiest way to parse json using gson when the element to parse is an element of a json string?
    primarykey
    data
    text
    <p>I am using gson to parse json into java beans. For the API I am using, a large number of the json results include the result as the first property of a json object. The "gson way" seems to be to create an equivalent wrapper java object, which has one property for the target output type - but this results in unnecessary throwaway classes. Is there a best practice way of doing this?</p> <p>For example to parse: <code>{"profile":{"username":"nickstreet","first_name":"Nick","last_name":"Street"}}</code></p> <p>I have to do:</p> <pre><code>public class ParseProfile extends TestCase { public void testParseProfile() { Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); String profileJson = "{\"profile\":{\"username\":\"nickstreet\",\"first_name\":\"Nick\",\"last_name\":\"Street\"}}"; ProfileContainer profileContainer = gson.fromJson(profileJson, ProfileContainer.class); Profile profile = profileContainer.getProfile(); assertEquals("nickstreet", profile.username); assertEquals("Nick", profile.firstName); assertEquals("Street", profile.lastName); } } public class ProfileContainer { protected Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } } public class Profile { protected String username; protected String firstName; protected String lastName; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } </code></pre> <p>Of course an alternative approach to using a container would be to manually remove the outer portion of the string (i.e. remove the "profile":{ and closing }) using standard string parsing techniques, but this feels like the wrong approach.</p> <p>I would like to be able to do something like:</p> <pre><code>Profile p = gson.fromJsonProperty(json, Profile.class, "profile"); </code></pre> <p><a href="http://code.google.com/p/google-gson/issues/detail?id=110" rel="noreferrer">This issue</a> suggests it should be possible to break a json string up into a json object, to pull out a jsonElement from this object, and pass it into json.fromJson(). However, the toJsonElement() method only works on a java object, not a json string.</p> <p>Does anyone have a better approach?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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