【问题标题】:How to ignore a JSON element in Android Retrofit如何在 Android Retrofit 中忽略 JSON 元素
【发布时间】:2015-08-10 14:29:00
【问题描述】:

我正在开发一个使用 Android Retrofit 发送 JSON 的 Android 应用程序(它将 POJO 类转换为 JSON 格式)。它工作正常,但我需要忽略从 POJO 类发送 JSON 一个元素。

有人知道任何 Android Retrofit 注释吗?

示例

POJO 类:

public class sendingPojo
{
   long id;
   String text1;
   String text2;//--> I want to ignore that in the JSON

   getId(){return id;}
   setId(long id){
     this.id = id;
   }

   getText1(){return text1;}
   setText1(String text1){
     this.text1 = text1;
   }

   getText2(){return text2;}
   setText2(String text2){
     this.text2 = text2;
   }


}

接口发送者 ApiClass

 public interface SvcApi {

 @POST(SENDINGPOJO_SVC_PATH)
 public sendingPojo addsendingPojo(@Body sendingPojo sp);

}

知道如何忽略 text2?

【问题讨论】:

  • 我最近在 Retrofit 库上工作。据我所知,如果您从 POJO 类中删除“text2”,则请求 json 不会将该字符串值映射到您的模型上。换句话说,它将被忽略。
  • 是的,当然可以,但是我需要 POJO 类中的这个“text2”变量才能在 android 应用程序中使用它,我只需要忽略 JSON 中的这个“text2”。

标签: android json retrofit


【解决方案1】:

如果您不想使用 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(),我找到了替代解决方案。

只需在我需要忽略的变量中包含transient。

所以,POJO 类终于来了:

public class sendingPojo {
    long id;
    String text1;
    transient String text2;//--> I want to ignore that in the JSON

    getId() {
        return id;
    }

    setId(long id) {
        this.id = id;
    }

    getText1() {
        return text1;
    }

    setText1(String text1) {
        this.text1 = text1;
    }

    getText2() {
        return text2;
    }

    setText2(String text2) {
        this.text2 = text2;
    }
}

希望对你有帮助

【讨论】:

  • 它是“transient String text2”而不是“String transient text2”。谢谢。
  • 我不确定这个答案是否比@nikhil.thakkar 写的更好。例如,当前类也可以实现 Serializable ,例如,我希望忽略 JSON 中的字段 text2 并且不要忽略序列化。
【解决方案2】:

用 @Expose 注解,如:

@Expose private String id;

省略您不想序列化的任何字段。然后以这种方式创建您的 Gson 对象:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

【讨论】:

  • 这可能是一个很好的选择,也许我会用它来解决我的问题,但我想做的是维护自动构建 JSON 的 Retrofit 接口。
  • 您可以在改造中设置自定义转换器。默认支持。参考这里square.github.io/retrofit
  • 通过在字段上使用 @Expose 注释并使用 GSON 构建器配置改造,您可以忽略以下元素: .addConverterFactory(GsonConverterFactory.create(GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() ))
【解决方案3】:

您可以通过将 GSONBuilder 中的 GSON 对象添加到您的 ConverterFactory 中来配置 Retrofit,请参见下面的示例:

private static UsuarioService getUsuarioService(String url) {
    return new Retrofit.Builder().client(getClient()).baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(getGson())).build()
            .create(UsuarioService.class);
}

private static OkHttpClient getClient() {
    return new OkHttpClient.Builder().connectTimeout(5, MINUTES).readTimeout(5, MINUTES)
            .build();
}

private static Gson getGson() {
    return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}

要忽略字段元素,只需将 @Expose(deserialize = false, serialize = false) 添加到您的属性或不添加,对于(反)序列化您的字段元素,您可以将具有空值的 @Expose() 注释添加到您的属性。

@Entity(indexes = {
        @Index(value = "id DESC", unique = true)
})
public class Usuario {

    @Id(autoincrement = true)
    @Expose(deserialize = false, serialize = false) 
    private Long pkey; // <- Ignored in JSON
    private Long id; // <- Ignored in JSON, no @Expose annotation
    @Index(unique = true)
    @Expose
    private String guid; // <- Only this field will be shown in JSON.

【讨论】:

    【解决方案4】:

    如果您使用的是 Kotlin + Retrofit + Moshi (我测试了这个) 如果您想有条件地忽略字段,可以将其设置为 null。

    data class  User(var id: String,  var name: string?)
    
    val user = User()
    user.id = "some id"
    user.name = null
    

    生成的 Json 将是

    user{
    "id": "some id"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2018-05-11
      • 2017-09-25
      • 2016-08-13
      • 2012-02-08
      • 2016-09-12
      相关资源
      最近更新 更多