I need to serialize a list of simple Java objects to JSON using Google Gson library.

目标:

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);

它可以工作,但是JSON对象的字段名是lowerCamelCase.如下所示:

[
  {
    "imageIndex": "1",
    "text": "Text 1"
  },
  {
    "imageIndex": "2",
    "text": "Text 2"
  }
]

How do I get a JSON with UpperCamelCase field name, like this:

[
  {
    "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?

推荐答案

摘自文件:

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.

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:

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.

Json相关问答推荐

try 将文本标记放置在条形图上的同一高度

为什么terraform不缩小这个策略JSON?'

我可以使用JQ来缩小数组中的json对象的范围吗?

德纳比可视化区域图表 Power BI

如何使用SQLite Trigger将JSON对象数组转换为新记录?

取消嵌套数组并将数组名称添加为附加字段

嵌套存储在字符串变量中的 JSON 对象

为什么解析的字典相等而腌制的字典不相等?

将具有多个级别的 json 读入 DataFrame [python]

提交后使用 Rails 7 结合 JSON 标签进行标记

在PowerShell中按时间戳过滤JSON

如何使用 Swiftui 判断 JSON 是否缺少键值对

杰克逊 2.0 和 Spring 3.1

如何对使用转换器的 Grails 服务进行单元测试?

单元测试球衣 Restful Services

按 JSON 数据类型 postgres 排序

从 HttpResponse 获取 json

IE中Json响应下载(7~10)

什么是类型和类型令牌?

使用 Retrofit 解析动态密钥 Json 字符串