【问题标题】:What is the easiest way to parse json using gson when the element to parse is an element of a json string?当要解析的元素是 json 字符串的元素时,使用 gson 解析 json 的最简单方法是什么?
【发布时间】:2011-04-15 17:32:00
【问题描述】:

我正在使用 gson 将 json 解析为 java bean。对于我使用的 API,大量的 json 结果包括结果作为 json 对象的第一个属性。 “gson 方式”似乎是创建一个等效的包装 java 对象,它具有目标输出类型的一个属性——但这会导致不必要的一次性类。有没有这样做的最佳实践方法?

例如解析: {"profile":{"username":"nickstreet","first_name":"Nick","last_name":"Street"}}

我必须做的:

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;
    }
}

当然,使用容器的另一种方法是使用标准字符串解析技术手动删除字符串的外部部分(即删除“profile”:{并关闭}),但这感觉像是错误的方法。

我希望能够做类似的事情:

Profile p = gson.fromJsonProperty(json, Profile.class, "profile");

This issue 建议应该可以将 json 字符串分解为 json 对象,从该对象中提取 jsonElement,并将其传递给 json.fromJson()。但是,toJsonElement() 方法仅适用于 java 对象,而不适用于 json 字符串。

有人有更好的方法吗?

【问题讨论】:

    标签: java json parsing serialization gson


    【解决方案1】:

    我遇到了同样的问题。基本上,您为避免所有容器类废话所做的只是使用 JSONObject 函数来检索您尝试反序列化的内部对象。请原谅我草率的代码,但它是这样的:

    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();
    }
    

    【讨论】:

    • 感谢您的回复。这不是很正确,但它使我得到了正确的答案。您不能直接使用字符串创建 JsonObject,您必须使用 JsonParser 来创建它。 JsonParser 解析器 = 新的 JsonParser(); JsonObject j = (JsonObject) parser.parse(json);
    猜你喜欢
    • 2012-07-28
    • 2011-08-22
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多