What is the easiest way to parse json using gson when the element to parse is an element of a json string?

非 Y 不嫁゛ 提交于 2019-12-09 08:46:09

问题


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?


回答1:


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:

GsonBuilder gsonb = new GsonBuilder()
Gson gson = gsonb.create();

JSONObject j;
JSONObject jo;

Profile p = null;

try {

    j = new JSONObject(convertStreamToString(responseStream));
    jo = j.getJSONObject("profile"); // now jo contains only data within "profile"
    p = gson.fromJson(jo.toString(), Profile.class);
    // now p is your deserialized profile object
}catch(Exception e) {
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/3769473/what-is-the-easiest-way-to-parse-json-using-gson-when-the-element-to-parse-is-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!