当要解析的元素是json字符串的元素时,使用gson解析json的最简单方法是什么? [英] What is the easiest way to parse json using gson when the element to parse is an element of a json string?
问题描述
我使用gson将json解析为java bean。对于我使用的API,大量的json结果包含结果作为json对象的第一个属性。 gson方法似乎是创建一个等价的包装java对象,它具有目标输出类型的一个属性 - 但这会导致不必要的一次性类。有没有最好的做法这样做?
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?
例如解析:
{profile:{username: nickstreet,first_name:Nick,last_name:Street}}
p>
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;
}
}
当然,使用容器的另一种方法是使用标准字符串解析技术手动删除字符串的外部部分(即删除profile:{和closing}),但这种感觉就像是错误的方法。
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");
这个问题表明应该可以将json字符串分解成json对象,从这个对象中取出jsonElement并将其传递给json .fromJson()。然而,toJsonElement()方法只适用于java对象,而不是json字符串。
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?
推荐答案
我遇到了同样的问题。基本上你做什么来避免所有的容器类废话只是使用JSONObject函数来检索你试图反序列化的内部对象。原谅我的蠢代码,但它是这样的:
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();
}
这篇关于当要解析的元素是json字符串的元素时,使用gson解析json的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!