对象序列化为JSON(使用Gson)。如何在UpperCamelCase中设置字段名称? [英] Object Serialization to JSON (using Gson). How to set field names in UpperCamelCase?

查看:1195
本文介绍了对象序列化为JSON(使用Gson)。如何在UpperCamelCase中设置字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Google Gson库将简单Java对象列表序列化为JSON。


$ b 对象:

  public class SimpleNode {
private String imageIndex;
私人字符串文本;

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;




$ b我写了下面的代码序列化: p>

  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
}
]

如何获得带有UpperCamelCase字段名称的JSON,如下所示:

  [
{
ImageIndex:1,
Text:Text 1
},
{
ImageIndex:2,
Text:Text 2
}
]

我认为我可以将成员变量重命名为UpperCamelCase,但可能有另一种方法吗?$ b $来自文档:



Gson支持一些预先定义的字段命名策略来转换标准Java字段名称(例如,以小写字母开头的驼峰名称---sampleFieldNameInJava)添加到Json字段名称(即sample_field_name_in_java或SampleFieldNameInJava)中。有关预定义命名策略的信息,请参阅FieldNamingPolicy类。

它还具有基于批注的策略,允许客户端基于每个字段定义自定义名称。请注意,基于注解的策略具有字段名称验证,如果提供了无效的字段名称作为注释值,则会引发运行时间异常。



以下是一个示例如何同时使用Gson命名策略功能:

  private class SomeObject {
@SerializedName(custom_naming)
private final String someField;

private final String someOtherField;
$ b $ 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} 
pre>

然而,对于你想要的,你可以使用它:

  Gson gson = new GsonBuilder()。setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); 

通过使用UPPER_CAMEL_CASE选项,您可以达到目标。


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

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:

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

解决方案

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.

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(使用Gson)。如何在UpperCamelCase中设置字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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