Java 对象序列化为 JSON(使用 Gson)。如何在 UpperCamelCase 中设置字段名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22096274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object Serialization to JSON (using Gson). How to set field names in UpperCamelCase?
提问by Frank59
I need to serialize a list of simple Java objects to JSON using Google Gson library.
我需要使用 Google Gson 库将简单 Java 对象列表序列化为 JSON。
The object:
物体:
public class SimpleNode {
private String imageIndex;
private String text;
public String getImageIndex() {
return imageIndex;
}
public void setImageIndex(String imageIndex) {
this.imageIndex = imageIndex;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
I have written the following code for serialization:
我已经编写了以下用于序列化的代码:
List<SimpleNode> testNodes = repository.getElements(0);
Gson gson = new Gson();
String jsonNodesAsString = gson.toJson(testNodes);
It works, but the field name of the JSON objects is lowerCamelCase. Like this:
它可以工作,但 JSON 对象的字段名称是 lowerCamelCase。像这样:
[
{
"imageIndex": "1",
"text": "Text 1"
},
{
"imageIndex": "2",
"text": "Text 2"
}
]
How do I get a JSON with UpperCamelCase field name, like this:
如何获得带有 UpperCamelCase 字段名称的 JSON,如下所示:
[
{
"ImageIndex": "1",
"Text": "Text 1"
},
{
"ImageIndex": "2",
"Text": "Text 2"
}
]
I think that I can rename member variables in to UpperCamelCase, but may be there is another way?
我认为我可以将成员变量重命名为 UpperCamelCase,但可能还有其他方法吗?
采纳答案by Ben Dale
Taken from the docs:
取自文档:
Gson supports some pre-defined field naming policies to convert the standard Java field names (i.e. camel cased names starting with lower case --- "sampleFieldNameInJava") to a Json field name (i.e. sample_field_name_in_java or SampleFieldNameInJava). See the FieldNamingPolicy class for information on the pre-defined naming policies.
Gson 支持一些预定义的字段命名策略,可以将标准的 Java 字段名称(即以小写开头的驼峰式名称---“sampleFieldNameInJava”)转换为 Json 字段名称(即 sample_field_name_in_java 或 SampleFieldNameInJava)。有关预定义命名策略的信息,请参阅 FieldNamingPolicy 类。
It also has an annotation based strategy to allows clients to define custom names on a per field basis. Note, that the annotation based strategy has field name validation which will raise "Runtime" exceptions if an invalid field name is provided as the annotation value.
它还具有基于注释的策略,允许客户在每个字段的基础上定义自定义名称。请注意,基于注释的策略具有字段名称验证,如果提供无效字段名称作为注释值,则会引发“运行时”异常。
The following is an example of how to use both Gson naming policy features:
以下是如何使用 Gson 命名策略功能的示例:
private class SomeObject {
@SerializedName("custom_naming")
private final String someField;
private final String someOtherField;
public SomeObject(String a, String b) {
this.someField = a;
this.someOtherField = b;
}
}
SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
String jsonRepresentation = gson.toJson(someObject);
System.out.println(jsonRepresentation);
======== OUTPUT ========
======== 输出 ========
{"custom_naming":"first","SomeOtherField":"second"}
However, for what you want, you could just use this:
但是,对于你想要的,你可以使用这个:
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
By using the UPPER_CAMEL_CASE option, you'll achieve your goal.
通过使用 UPPER_CAMEL_CASE 选项,您将实现您的目标。