简体   繁体   English

对象序列化为JSON(使用Gson)。如何在UpperCamelCase中设置字段名称?

[英]Object Serialization to JSON (using Gson). How to set field names in UpperCamelCase?

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. 它可以工作,但JSON对象的字段名称是lowerCamelCase。 Like this: 像这样:

[
  {
    "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,但可能还有另一种方法吗?

Taken from the docs: 取自文档:

Gson supports some pre-defined field naming policies to convert the standard Java field names (ie camel cased names starting with lower case --- "sampleFieldNameInJava") to a Json field name (ie sample_field_name_in_java or SampleFieldNameInJava). Gson支持一些预定义的字段命名策略,以将标准Java字段名称(即以小写字母开头的驼峰名称---“sampleFieldNameInJava”)转换为Json字段名称(即sample_field_name_in_java或SampleFieldNameInJava)。 See the FieldNamingPolicy class for information on the pre-defined naming policies. 有关预定义命名策略的信息,请参阅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选项,您将实现目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM