本文介绍了是否有可能使用GSON将具有句点的JSON属性名称反序列化为嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是我尝试使用GSON消耗的那种JSON的一个示例: $ b {person:{name:Philipfather.name:Yancy} } 我想知道是否有可能将此JSON反序列化为以下结构: public class Person { private String name; 私人父亲父亲; } public class Father { private String name; $ b $ p $ b 所以: $ b p.name ==Philip p.father.name ==Yancy 目前我正在使用 @SerializedName 来获取包含句点的属性名称,例如: public class Person { private String name; @SerializedName(father.name) private String fathersName; } 然而,这并不理想。 从查看文档看来,它似乎不是立即可能的,但可能有一些我错过了 - 我是使用GSON的新手。 不幸的是,我无法更改我正在使用的JSON,而且我不愿意切换到另一个JSON解析库。 解决方案据我了解,您不能以直接方式执行此操作,因为Gson会理解 father.name 作为单个字段。 您需要编写自己的自定义解序器。请参阅Gson用户指南说明 此处 。 我从来没有尝试过它,但似乎并不难。此 发布 可能也有帮助。 看看Gson的用户指南和该帖子中的代码,您需要这样的内容: private class PersonDeserializer实现了JsonDeserializer< Person> { @Override public Person deserialize(JsonElement json,Type type, JsonDeserializationContext context)throws JsonParseException { JsonObject jobject =(JsonObject)json ; 父亲父亲=新父亲(jobject.get(father.name)。getAsString()); 返回新的Person(jobject.get(name)。getAsString(),father); $ b假设你有合适的构造函数... $> 然后: GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Person.class,new PersonDeserializer()); Gson gson = gsonBuilder.create(); Person person = gson.fromJson(jsonString,Person.class); Gson会调用您的反序列化器来将JSON反序列化为 Person object。 注意:我没有试过这段代码,但应该是这样或者非常相似。 em> This is an example of the kind JSON I'm trying to consume using GSON:{ "person": { "name": "Philip" "father.name": "Yancy" }}I was wondering if it were possible to deserialize this JSON into the following structure:public class Person{ private String name; private Father father;}public class Father{ private String name;}So that:p.name == "Philip"p.father.name == "Yancy"Currently I am using @SerializedName to obtain property names containing a period, e.g.:public class Person{ private String name; @SerializedName("father.name") private String fathersName;}However, that's not ideal.From looking at the documentation it doesn't appear to be immediately possible but there may be something I have missed - I'm new to using GSON.Unfortunately I cannot change the JSON I'm consuming and I'm reluctant to switch to another JSON parsing library. 解决方案 As far as I understand you can't do it in a direct way, because Gson will understand father.name as a single field.You need to write your own Custom Deserializer. See Gson user's guide instructions here.I've never tried it, but it doesn't seem to be too difficult. This post could be also helpful.Taking a look at Gson's user guide and the code in that post, you'll need something like this:private class PersonDeserializer implements JsonDeserializer<Person> { @Override public Person deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jobject = (JsonObject) json; Father father = new Father(jobject.get("father.name").getAsString()); return new Person(jobject.get("name").getAsString(), father); }}Assuming that you have suitable constructors...And then:GsonBuilder gsonBuilder = new GsonBuilder();gsonBuilder.registerTypeAdapter(Person.class, new PersonDeserializer());Gson gson = gsonBuilder.create();Person person = gson.fromJson(jsonString, Person.class);And Gson will call your deserializer in order to deserialize the JSON into a Person object.Note: I didn't try this code, but it should be like this or something very similar. 这篇关于是否有可能使用GSON将具有句点的JSON属性名称反序列化为嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 16:47