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?
For example to parse:
{"profile":{"username":"nickstreet","first_name":"Nick","last_name":"Street"}}
I have to do:
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;
}
}
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.
I would like to be able to do something like:
Profile p = gson.fromJsonProperty(json, Profile.class, "profile");
This issue 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.
Does anyone have a better approach?
I ran into the same problem. Basically what you do to avoid all the container class nonsense is just use JSONObject functions to retrieve the inner-object that you are trying to deserialize. Forgive me for the sloppy code but it goes something like this: