GSON 2.0+从两个不同的序列化字段名称中反序列化一个字段 [英] GSON 2.0+ deserialize a field from either of two different serialized field names

查看:557
本文介绍了GSON 2.0+从两个不同的序列化字段名称中反序列化一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected double a = 0.0;在我的Java类中,我有一个字段声明如下: 

在反序列化的JSON中重构此类,该字段可以与两个不同的名字(遗留问题)。例如,JSON字段可能如下所示:

 a:9.57,



或者像这样:

  d:9.57,



<幸运的是,遗留名称d不会导致命名冲突)



我的问题是我需要用JSON键a或d填充类字段a - 无论是哪一个当下。 (我相信它们总是互相排斥的,但我并没有真正证明这一点。)



我在Netbeans中使用Gson 2.2.1和Java 7。

解决方案

您需要 JsonDeserializer ,您可以在JSON字符串中检查特定键并根据其存在性简单地在您的定制POJO类中设置值如下图所示。



欲了解更多信息,请参阅 GSON Deserialiser Example



示例代码:

  class MyJSONObject {
protected double a = 0.0;

public double getA(){
return a;
}

public void setA(double a){
this.a = a;
}

}

类MyJSONObjectDeserializer实现了JsonDeserializer< MyJSONObject> {
$ b $ @Override
public MyJSONObject deserialize(final JsonElement json,final Type typeOfT,$ b $ final JsonDeserializationContext context)throws JsonParseException {

JsonObject jsonObject = json .getAsJsonObject();

MyJSONObject对象= new MyJSONObject();

if(jsonObject.get(a)!= null){
object.setA(jsonObject.get(a)。getAsDouble());
} else if(jsonObject.get(d)!= null){
object.setA(jsonObject.get(d)。getAsDouble());
}

返回对象;
}
}

...

字符串json ={\a \:\9.57\} ;
// String json ={\d \:\9.57\};
MyJSONObject data = new GsonBuilder()
.registerTypeAdapter(MyJSONObject.class,new MyJSONObjectDeserializer())。create()$ b $ .fromJson(json,MyJSONObject.class);

System.out.println(data.getA());


In my Java class I have a field declared like this:

protected double a = 0.0;

In the JSON that is deserialized to reconstitute this class, that field can appear with either of two different names (legacy issue). As an example, the JSON field can look like this:

"a": 9.57,

or like this:

"d": 9.57,

(Luckily, the legacy name "d" does not cause a naming collision with any other variables.)

My problem is that I need to populate the class field "a" with either the JSON key "a" or "d" -- whichever is present. (I believe they are always mutually exclusive, but I have not actually proven that beyond all doubt.)

I'm using Gson 2.2.1 and Java 7 in Netbeans.

解决方案

You need JsonDeserializer where you can check the specific key in the JSON string and based on its presence simply set the value in your custom POJO class as shown below.

For more info have a look at GSON Deserialiser Example

Sample code:

class MyJSONObject {
    protected double a = 0.0;

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

}

class MyJSONObjectDeserializer implements JsonDeserializer<MyJSONObject> {

    @Override
    public MyJSONObject deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {

        JsonObject jsonObject = json.getAsJsonObject();

        MyJSONObject object = new MyJSONObject();

        if (jsonObject.get("a") != null) {
            object.setA(jsonObject.get("a").getAsDouble());
        } else if (jsonObject.get("d") != null) {
            object.setA(jsonObject.get("d").getAsDouble());
        }

        return object;
    }
}

...

String json = "{\"a\":\"9.57\"}";
// String json = "{\"d\":\"9.57\"}";
MyJSONObject data = new GsonBuilder()
        .registerTypeAdapter(MyJSONObject.class, new MyJSONObjectDeserializer()).create()
        .fromJson(json, MyJSONObject.class);

System.out.println(data.getA());

这篇关于GSON 2.0+从两个不同的序列化字段名称中反序列化一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆