简体   繁体   中英

How to handle optional JSON fields in Retrofit for Android?

I am working on a JSON parser for an Android application. When I call the server for data, there are some optional fields, how do I handle this in Retrofit using GSON converter?

Normal response

{
   "status":"SUCCESS",
   "class-1":{
      "class.enddate":"Jan/10/2016",
      "class.startdate":"Jan/10/2015",
      "class.title":"Physics 1",
      "class.short.description":"Physics 1",
      "class.description":"This is a Physics Class"
   }
}

Alternate response, when some fields do not have any data

{
  "status":"SUCCESS",
  "class-1":{
     "class.enddate":"Jan/10/2016",
     "class.startdate":"Jan/10/2015",
     "class.title":"Physics 1"
   }
}

POJO Classes

public class MyClass {
    @Expose @SerializedName("status")
    public String status;

    @Expose @SerializedName("class-1")
    public MyClassInformation myClassInformation;
}

public class MyClassInformation {
    @Expose @SerializedName("class.title")
    public String classTitle;

    @Expose @SerializedName("class.short.description")
    public String classShortDescription;

    @Expose @SerializedName("class.description")
    public String classDescription;

    @Expose @SerializedName("class.startdate")
    public String startDate;

    @Expose @SerializedName("class.enddate")
    public String endDate;
}

How do I create the POJO classes in a way to handle the optional fields not being present? At the moment the whole MyClassInformation object becomes NULL when fields become missing, Please help.

I managed to solve this by trial and error, managed to get it working by removing the @Expose annotation and changing the Gson constructor... Now the whole object does not get nulled or excluded if fields are missing and only the missing fields show up as null.

This is what I changed, From

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

To

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

Hope it helps anyone, who is looking for a similar answer.

I struggled with this couple of years ago.

Probably there is no good answer to this. At least not that I am aware of.

3 options considered:

  1. Set all the members as must-fields (assuming they all known in advance) and set them to null in case no value received where the type make sense (it does not always make sense for primitive types for instance). Here you put more work on the ones who pass you the JSON but your java code is simpler, however, less flexible.
  2. Set the optional fields inside a container of key-value pairs where both the key and the value are Strings. The container can be an empty list if no optional properties were passed. Here you have more flexibility but have to work harder creating custom (de)serializers.
  3. Use a combination of 1 and 2.

Every option has pros and cons and I ended up with option 3.

Hope it helps and good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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