简体   繁体   English

如何在使用Gson序列化公开字段时更改字段名称?

[英]How to change name of fields when serialized exposed fields using Gson?

I am using Gson to serialize a Java object and return a json string. 我正在使用Gson序列化Java对象并返回一个json字符串。 The object has many fields, but I'm trying to return 4 in particular. 该对象有很多字段,但我特别想要返回4。 I'm using the @Expose annotation to tell Gson to ignore any other fields, but I would also like to be able to change the name of these fields to be returned. 我正在使用@Expose注释告诉Gson忽略任何其他字段,但我也希望能够更改要返回的这些字段的名称。 Using GsonBuilder this way 以这种方式使用GsonBuilder

Gson gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();

I am able to only get only fields with @Expose . 我只能使用@Expose获取字段。 Is there a way to use @SerializedName along with @Expose , to change the name of these fields before they are returned? 有没有办法使用@SerializedName@Expose ,在返回之前更改这些字段的名称? Using both annotations blocks anything from being returned, but I'm also finding that using just the @SerializedName annotation (and removing .excludeFieldsWithoutExposeAnnotation() ) also prevents those fields from being returned. 使用两个注释阻止返回任何内容,但我也发现只使用@SerializedName注释(并删除.excludeFieldsWithoutExposeAnnotation() )也会阻止返回这些字段。

You are on the right track. 你走在正确的轨道上。 Here's a working example: 这是一个有效的例子:

package test;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class MyData {
  @Expose
  private String id;

  @Expose
  @SerializedName("fileOriginalName")
  private String myFilename;

  @Expose
  @SerializedName("fileOriginalPath")
  private String myPath;

  private String myName;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getMyFilename() {
    return myFilename;
  }

  public void setMyFilename(String myFilename) {
    this.myFilename = myFilename;
  }

  public String getMyPath() {
    return myPath;
  }

  public void setMyPath(String myPath) {
    this.myPath = myPath;
  }

  public String getMyName() {
    return myName;
  }

  public void setMyName(String myName) {
    this.myName = myName;
  }

}

And the caller: 和来电者:

package test;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class Demo {
  static final Type MYDATA_TYPE = new TypeToken<MyData>() {
  }.getType();
  public static void main(String[] args){
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();

    MyData d = new MyData();

    d.setId("10");
    d.setMyFilename("was called myFilename");
    d.setMyName("should not be visible");
    d.setMyPath("was called myPath");

    String json = gson.toJson(d, MYDATA_TYPE);
    System.out.println(json);
  }
}

Here is the output: 这是输出:

{
  "id": "10",
  "fileOriginalName": "was called myFilename",
  "fileOriginalPath": "was called myPath"
}

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

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