GSON Java如何在映射和打印之间使用不同的名称? [英] GSON java how can i make different names between mapping and printing?

查看:74
本文介绍了GSON Java如何在映射和打印之间使用不同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的JSON:

I have a JSON like this:

{
  "aggregation": {
    "CityAgg" : {
      "key" : "Paris"
    }
  }
}

我创建映射,并为每个字段添加一个@SerializedName,因为我想为变量创建自定义名称.

I create mapping and for each field, I add a @SerializedName because I want to create custom names for my variables.

例如在JSON中,有一个键名key,但是我希望Java中的变量为cityName.

For example in the JSON, there is a key names key, but I want my variable in Java to be cityName.

所以,我这样做:

@SerializedName("key")
public String cityName

我可以像这样将响应JSON动态映射到我的对象:

I can dynamically map the response JSON to my objects like this:

Gson gson = new Gson();
Query2 query2 = gson.fromJson(response, Query2.class);

它运行良好.

但是,当我要打印映射的对象时,请执行以下操作:

However, when I want to print the mapped object, I do this:

String query2String = gson.toJson(query2);
Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gsonPretty.toJson(query2String);

问题在于,在prettyJson中,我可以看到key,而不是cityName.

The problem is that, in the prettyJson, I can see the key, not cityName.

我想知道是否有一种自定义方法.我不想看到key.

I would like to know if there is a way to customize that. I don't want to see key.

推荐答案

如果您不想将自定义序列化器/反序列化器类编写为

If you do not want to write custom serializer / deserializer class as Viswanath Lekshmanan has mentioned, you can always create some kind of DTO object.

您的域类:

class CityAgg {
    @SerializedName("key")
    protected String cityName;

    public String getCityName() {
        return cityName;
    }
}

class Aggregation {
    @SerializedName("CityAgg")
    protected CityAgg cityAgg;

    public CityAgg getCityAgg() {
        return cityAgg;
    }
}

class Query2 {
    @SerializedName("aggregation")
    protected Aggregation aggregation;

    public Aggregation getAggregation() {
        return aggregation;
    }
}

DTO类示例:

class CityAggDto {
    protected String cityName;

    public CityAggDto(CityAgg query2) {
        this.cityName = query2.getCityName();
    }
}

样品使用:

String response = "{ \"aggregation\": { \"CityAgg\": { \"key\": \"value\" } } }";

Gson gson = new Gson();
Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();

Query2 query2 = gson.fromJson(response, Query2.class);
CityAggDto cityAggDto = new CityAggDto(query2.getAggregation().getCityAgg());

String cityAggDtoJson = gson.toJson(cityAggDto);
String cityAggDtoPrettyJson = gsonPretty.toJson(cityAggDto);

Log.d(TAG, "onCreate: " + cityAggDtoJson);
Log.d(TAG, "onCreate: " + cityAggDtoPrettyJson);

结果:

@SerializationName批注同时用于序列化和反序列化这两个操作,这就是您的JSON字符串包含key密钥的原因.

@SerializationName annotation is being used for both operations - serialization and deserialization - that is why your JSON strings contains key key.

可悲的是,无论@SerializationName注释是否可以与方法一起使用-由于GSON仅基于字段,因此对getter和setter使用不同的值将不起作用.

It is sad that, regardless of that @SerializationName annotation can be used with methods - using different values for getters and setters won't work as GSON is fields-based only:

使用字段vs吸气剂指示Json元素

Using fields vs getters to indicate Json elements

某些Json库使用一种类型的getter来推断Json元素.我们选择使用所有非临时,静态或综合字段(在继承层次结构中).我们这样做是因为并非所有类都使用适当命名的getter编写.而且,getXXX或isXXX可能是语义的,而不是指示属性.

Some Json libraries use the getters of a type to deduce the Json elements. We chose to use all fields (up the inheritance hierarchy) that are not transient, static, or synthetic. We did this because not all classes are written with suitably named getters. Moreover, getXXX or isXXX might be semantic rather than indicating properties.

但是,也有很好的论据来支持属性.我们打算在以后的版本中增强Gson以支持属性,作为表示Json字段的替代映射.目前,Gson是基于字段的.

However, there are good arguments to support properties as well. We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields. For now, Gson is fields-based.

来源: https://github.com/google/gson/blob /master/GsonDesignDocument.md

这就是为什么我改用杰克逊的原因. :)

That's why I have switched to Jackson. :)

这篇关于GSON Java如何在映射和打印之间使用不同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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