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?

2
  • what about this: stackoverflow.com/a/17089654/4310784 Commented Apr 25, 2016 at 10:11
  • can you post class (if any) which you are using to de/serialize.. Commented Apr 25, 2016 at 12:46

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I was looking for a Flag, not a hack. But I'll try it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.