0

How can I say to GSON that if a JSON with an array field has NULL value, it has to create an empty array instead of setting a null?

are there any properties or Flags available for this?

Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60

1 Answers1

1

You can do some workaround before you pass your json to Gson.

String currentKeyTags = "\"KeyTags\":null";
String expectedKeyTags = "\"KeyTags\":[]";

String jsonArrayString= jsonArray.toString()
                        .replaceAll(currentKeyTags, expectedKeyTags);

Now:

 Gson gson=new Gson();
     Type listType = new TypeToken<List<?>>() {
                }.getType();
    List<?> lists = gson.fromJson(jsonArrayString, listType);
Chaitu
  • 907
  • 2
  • 13
  • 27