【问题标题】:JSONObject fields in GSON class - Remains emptyGSON 类中的 JSONObject 字段 - 保持为空
【发布时间】:2018-03-29 04:01:15
【问题描述】:

Gson 类可以包含 JSONObject 类型的字段吗? 这是我的 GSON 课程

  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? = ""
}

JSONObject 字段 segmentsaggregate 在序列化时仍然是空的 JSONObject(在 Retrofit 中使用默认的 GsonConverterFactory)。这就是我得到的。有什么建议可以写吗?

{"items":[{"agr":{},"name":"Logged In","seg":{},"ts":"2017-10-17T12:20:32Z","type":"event"}]}

【问题讨论】:

标签: android json serialization gson retrofit


【解决方案1】:

解决方法 - 用 HashMap() 替换 JSONObject 帮助我获得了相同的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多