Search code examples
androidjsongsonpojo

In GSON, how to work with JSON attributes containing space?


Since GSON sets the object(pojo)'s fields using reflection, what if the json attributes contain spaces? Could we solve it without telling the server?

Example :

"node": {
            "Description": "Traffic Accident",
            "Date of Event": "02 September 2015",
            "Location": "Naypyitaw›Tatkon",
            "Type": "blah blah",
        }

You can't name the field as "Date of Event" in java pojo.


Solution

  • You can parse JSON keys with whitespace with Gson by using SerializedName annotation.

    Example:

    @SerializedName("Date of Event") public String dateOfEvent;