本文介绍了Java-字段名称的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个对象:
Object A
String field1 = "abc";
String field2 = "xyz";
上面的json是:
{
"ObjectA": {
"field1": "abc",
"field2": "xyz"
}
}
在发送json之前,我试图为字段名称创建新的ID.例如. "field1"将被称为"f1","field2"将被称为"f2".因此预期的输出json如下所示:
I was trying to create a new id for the field names before sending the json. E.g. "field1" to be called "f1" and "field2" to be called "f2". So the intended output json is shown below:
{
"ObjectA": {
"f1": "abc",
"f2": "xyz"
}
}
我不确定该怎么做.以上可以干净的方式完成吗?感谢您的帮助和指导.
I am not sure how to do this. Can the above be done in a clean way? Thanks for your help and pointers.
我正在使用gson.
推荐答案
在字段中使用注释@SerializedName("name")
.像这样:
Use the annotation @SerializedName("name")
on your fields. Like this:
Object A
@SerializedName("f1")
String field1 = "abc";
@SerializedName("f2")
String field2 = "xyz";
请参见 https://google.github.io /gson/apidocs/com/google/gson/annotations/SerializedName.html .
这篇关于Java-字段名称的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!