【问题标题】:Can't convert JSON to object无法将 JSON 转换为对象
【发布时间】:2020-10-01 18:56:08
【问题描述】:

Json 响应如下所示:

{
    "status": 1,
    "data": [
        [
            {
                "id": "4iQDR9r1Ch",
                "body": "test test",
                "da": "1601575850",
                "dm": "1601575850"
            }
        ]
    ]
}

还有我的课:

data class NotesListResponse(
    @SerializedName("status") val status: Int,
    @SerializedName("data") val data: List<List<NoteResponse>>)

data class NoteResponse(
    @SerializedName("id") val id: String,
    @SerializedName("body") val body: String,
    @SerializedName("da") val da: String,
    @SerializedName("dm") val dm: String
)

错误信息:com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 48 path $.data[0][0].body

怎么了? JSON 有效,并检查了类的正确性

【问题讨论】:

  • 您是否有权访问Gson 类,例如Gson gson = new Gson();
  • @RossSheppard 是的,其他转换(使用其他 json 和类型)完美运行
  • 您是否尝试过将Gson 置于lenient 模式?这可能有助于查看响应中是否存在 Gson 不喜欢的字符。我添加了一个答案,但如果这不起作用,请告诉我!
  • 我刚用过它,它有效,你确定你得到的 JSON 是那个吗?

标签: json kotlin gson


【解决方案1】:

如果您有权访问 Gson 类,请尝试以下操作。 lenient 模式可以让你看看它是否有效,还有非lenient 模式无法解析的字符。

Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(<insert response>));
reader.setLenient(true);

【讨论】:

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