首页  >  文章  >  Java  >  在Java中使用Gson库,如何将FieldNamingPolicy枚举翻译成中文?

在Java中使用Gson库,如何将FieldNamingPolicy枚举翻译成中文?

王林
王林转载
2023-08-23 19:33:031049浏览

在Java中使用Gson库,如何将FieldNamingPolicy枚举翻译成中文?

Gson库提供了作为枚举的一部分的命名约定。我们可以使用类的方法设置字段命名策略。

FieldNamingPolicy枚举常量

  • IDENTITY - 使用此命名策略,字段名称不变。
  • LOWER_CASE_WITH_DASHES - 使用此命名策略,将Java字段名称从驼峰形式修改为小写字段名称,每个单词之间用破折号(-)分隔。
  • LOWER_CASE_WITH_UNDERSCORES - 使用此命名策略,将Java字段名称从驼峰形式修改为小写字段名称,每个单词之间用下划线(_)分隔。
  • UPPER_CAMEL_CASE - 使用此命名策略,确保将Java字段名称序列化为其JSON形式时,首字母大写。
  • UPPER_CAMEL_CASE_WITH_SPACES - 使用此命名策略,确保将Java字段名称序列化为其JSON形式时,首字母大写,单词之间用空格分隔。

Syntax

public enum FieldNamingPolicy extends Enum<FieldNamingPolicy> implements FieldNamingStrategy

示例

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonFieldNamingPolicyTest {
   public static void main(String[] args) {
      Employee emp = new Employee();
      emp.setEmpId(115);
      emp.setFirstName("Raja");
      emp.setLastName("Ramesh");
      GsonBuilder gsonBuilder = new GsonBuilder();
      Gson gson1  =  gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
      String result1 = gson1.toJson(emp);
      System.out.println("LOWER_CASE_WITH_DASHES: " + result1);
      Gson gson2 =  gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
      String result2 = gson2.toJson(emp);
      System.out.println("LOWER_CASE_WITH_UNDERSCORES: " + result2);
      Gson gson3 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
      String result3 = gson3.toJson(emp);
      System.out.println("UPPER_CAMEL_CASE: " + result3);
      Gson gson4 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES).create();
      String result4 = gson4.toJson(emp);
      System.out.println("UPPER_CAMEL_CASE_WITH_SPACES: " + result4);
      Gson gson5 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
      String result5 = gson5.toJson(emp);
      System.out.println("IDENTITY: " + result5);
   }
}
// Employee class
class Employee {
   private int empId;
   private String firstName;
   private String lastName;
   public int getEmpId() {
      return empId;
   }
   public void setEmpId(int empId) {
      this.empId = empId;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}

输出

LOWER_CASE_WITH_DASHES: {"emp-id":115,"first-name":"Raja","last-name":"Ramesh"}
LOWER_CASE_WITH_UNDERSCORES: {"emp_id":115,"first_name":"Raja","last_name":"Ramesh"}
UPPER_CAMEL_CASE: {"EmpId":115,"FirstName":"Raja","LastName":"Ramesh"}
UPPER_CAMEL_CASE_WITH_SPACES: {"Emp Id":115,"First Name":"Raja","Last Name":"Ramesh"}
IDENTITY: {"empId":115,"firstName":"Raja","lastName":"Ramesh"}

以上是在Java中使用Gson库,如何将FieldNamingPolicy枚举翻译成中文?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除