简体   繁体   English

通过GSON处理相同名称的多个属性

[英]Handling multiple attribute of same name via GSON

I am using Gson for maping from JSON in my android application. 我在Android应用程序中使用Gson从JSON映射。 I have an issue where I have call eg 我有一个可以打电话给的问题,例如

public class ABC {

    XYZ image;
}

This image Object is of XYZ class but the image object from server is a String object. 该图像对象是XYZ类,但是来自服务器的图像对象是String对象。 I want Gson to ignore this XYZ image attribute but to map image in other attribute. 我希望Gson忽略此XYZ图像属性,而将图像映射到其他属性。 like 喜欢

public class ABC {

    @SerializedName("image")
    String imageUrl

    XYZ image;
}

It is possible to do this like above? 可以像上面那样做吗? Your help will be highly appreciated. 非常感谢您的帮助。 Thanks 谢谢

Annotate the fields you want to expose with @Expose and create your Gson instance with the builder, setting the appropriate configuration 使用@Expose注释要公开的字段,并使用构建器创建Gson实例,并设置适当的配置

public class ABC {    
    @Expose
    @SerializedName("image")
    String imageUrl

    XYZ image;
}
...
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

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

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