Android 改进最佳实践,定制gson反序列化器
我有一个问题,关于如何通过改造来执行这种反序列化 我需要解析一个复杂的Json:Android 改进最佳实践,定制gson反序列化器,android,gson,retrofit,json-deserialization,Android,Gson,Retrofit,Json Deserialization,我有一个问题,关于如何通过改造来执行这种反序列化 我需要解析一个复杂的Json: { "key":"value", "rows" : [ { //type 1 object "type" : "the_type_1", "self" :{ //type 1 subobject } }, { //type 2 object "type" : "the_type_2", "self"
{
"key":"value",
"rows" : [
{ //type 1 object
"type" : "the_type_1",
"self" :{
//type 1 subobject
}
},
{ //type 2 object
"type" : "the_type_2",
"self" : {
//type 2 subobject
}
},
{ //type 3 object
"type" : "the_type_3",
"self" : { //type 2 object with different key
"type" : "the_type_2",
"target":{
//type 2 subobject
}
}
}
]
}
我的每一行都可以有非常不同的内容shemes,我已经对其进行了简化,但是您可以看到,我有一些嵌套对象,这些对象的一些键根据它在json中的深度进行了更改
因此,我的第一个解决方案是构建庞大的Gson模型,其中包含各种可能性的每个键,然后测试“类型”类型的字段,以了解访问什么。但它很难维护,所以不优雅
我想我可以做一些与和自定义改装转换器,但我不知道到底从哪里开始,也不知道这是正确的方法
你会怎么做
另外,我的帖子标题不好,因为我没有想出更好的,请提出建议
谢谢,更新了 上这个班
public class Target {
}
让这个Self.java
import com.google.gson.annotations.SerializedName;
public class Self {
@SerializedName("type")
private String type;
@SerializedName("target")
private Target target;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Target getTarget() {
return target;
}
public void setTarget(Target target) {
this.target = target;
}
}
import com.google.gson.annotations.SerializedName;
public class Row {
@SerializedName("type")
private String type;
@SerializedName("self")
private Self self;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Self getSelf() {
return self;
}
public void setSelf(Self self) {
this.self = self;
}
}
制作一个Row.java
import com.google.gson.annotations.SerializedName;
public class Self {
@SerializedName("type")
private String type;
@SerializedName("target")
private Target target;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Target getTarget() {
return target;
}
public void setTarget(Target target) {
this.target = target;
}
}
import com.google.gson.annotations.SerializedName;
public class Row {
@SerializedName("type")
private String type;
@SerializedName("self")
private Self self;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Self getSelf() {
return self;
}
public void setSelf(Self self) {
this.self = self;
}
}
这是Respones.java
,它将使用gson进行序列化
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Respones {
@SerializedName("key")
private String key;
@SerializedName("rows")
private ArrayList<Row> rows;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public ArrayList<Row> getRows() {
return rows;
}
public void setRows(ArrayList<Row> rows) {
this.rows = rows;
}
}
输出
{
"key": "value",
"rows": [
{
"type": "the_type_1",
"self": {
}
},
{
"type": "the_type_2",
"self": {
}
},
{
"type": "the_type_3",
"self": {
"type": "the_type_2",
"target": {
}
}
}
]
}
value
the_type_1
the_type_2
嗨,谢谢你花时间回答我这个问题。我无法通过改造实现这一点,我无法访问原始json。还请注意,我试图从json创建pojo,而不是相反。@RenaudFavier,嗯,我理解,我发布的上述代码实际上需要程序员知道json的结构,其中键将由Java类中的属性表示,我不知道Pojo是如何工作的,但我想知道,因为很多人都在使用它,实际上,这段代码可以用来阅读您发布的以下JSON,它将填充您的对象,我将更新答案,以便您可以看到it@RenaudFavier,我检查了POJO是什么,我能说的是,您最好使用我提供的代码。但是,如果你想拥有POJO,你可以让类的所有实例变量公开,删除你不需要的getter和setter,删除注释,这样你就可以从我编写的类中得到你想要的POJO。我已经做了一个自定义解析器,它工作正常(我使用gson流模式),问题是与改造的集成。你的代码与我想要的相反(从json到java对象)@RenaudFavier,你是说你想要java对象到json