0

Can Gson classes contain fields of type JSONObject? Here is my GSON class

  class Item {
  @Expose
  var name: String? = ""
  @Expose
  var type: String? = ""
  @Expose
  @SerializedName("agr")
  var aggregate: JSONObject? = null
  @Expose
  @SerializedName("seg")
  var segments: JSONObject? = null
  @Expose
  @SerializedName("ts")
  var timestamp: String? = ""
}

The JSONObject fields segments and aggregate remains an empty JSONObject when serialised (in Retrofit using default GsonConverterFactory). here's what I got. Any suggestions to get it write?

{"items":[{"agr":{},"name":"Logged In","seg":{},"ts":"2017-10-17T12:20:32Z","type":"event"}]}
vivek.kartha
  • 308
  • 1
  • 3
  • 14
  • you need a custom deserializer, you can find more info here: https://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Jibbo Oct 17 '17 at 12:53

1 Answers1

0

A work around - Replacing the JSONObject with a HashMap() had helped me achieve the same results.

vivek.kartha
  • 308
  • 1
  • 3
  • 14