【问题标题】:GSON fromJSON Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $GSON fromJSON 预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY
【发布时间】:2020-10-09 03:00:47
【问题描述】:

我正在尝试将字符串转换为 JSONArray,但遇到了问题。

这是我的测试:

class MainActivityTest {

    @Test
    fun checkParse(){
        val loader = ClassLoader.getSystemClassLoader()
        val json: String = Files.lines(
            get(
                loader.getResource("data.json").toURI()
            )
        )
            .parallel()
            .collect(Collectors.joining())
        val main = MainActivity()
        val dataParse2 = Gson().fromJson(json, JSONArray::class.java)
        val gson = GsonBuilder().create()
        val parse2 = gson.fromJson(json, Array<QuoteModel>::class.java).toList()
        val parse1 = main.parseResponse(dataParse2)

        assertEquals(parse1,parse2)
    }


}

这是我在 MainActivity 中测试的函数:

fun parseResponse(response: JSONArray): List<QuoteModel> {
    val fileData = response.toString()
    val gson = GsonBuilder().create()
    return gson.fromJson(fileData, Array<QuoteModel>::class.java).toList()
}

这是我的 data.json 文件:

[
  {
    "text": "Genius is one percent inspiration and ninety-nine percent perspiration.",
    "author": "Thomas Edison"
  },
  {
    "text": "You can observe a lot just by watching.",
    "author": "Yogi Berra"
  },
  {
    "text": "A house divided against itself cannot stand.",
    "author": "Abraham Lincoln"
  }
]

这个问题来自我的测试:

val dataParse2 = Gson().fromJson(json, JSONArray::class.java)

有什么想法吗?


首次更新

这是我在 MainActivity 中调用 parseFunction 的函数:

private fun jsonArrayRequest(url: String): JsonArrayRequest {
    return JsonArrayRequest(Request.Method.GET, url, null,
        { response ->

            val quotesArray = parseResponse(response)
            displayQuote(chooseQuote(quotesArray))
        },
        { error ->
            TODO("Handle error missing")
        }
    )
}

【问题讨论】:

    标签: kotlin gson


    【解决方案1】:
    private fun jsonArrayRequest(url: String): JsonArrayRequest {
    return JsonArrayRequest(Request.Method.GET, url, null,
            { response ->
    
                val responseArray = JSONArray(response)
                val quotesArray = parseResponse(responseArray)
                displayQuote(chooseQuote(quotesArray))
            },
            { error ->
                TODO("Handle error missing")
            }
    )
    
    fun parseResponse(response: JSONArray): List<QuoteModel> {
    return Gson().fromJson(response.toString(), Array<QuoteModel>::class.java).toList()
    

    【讨论】:

    • 当我尝试这样做时,我收到了这条消息:类型不匹配。必需:JSONArray,找到:JsonArray! .在行中: val parse1 = main.parseResponse(dataParse2)
    • dataParse2 那里是红色的
    • 在 parseResponse 方法中使用相同的类型:parseResponse(response: JsonArray)
    • 我在主要问题中添加了显示另一个功能的信息,所以我不能这样做,因为我正在处理请求的响应。如果我这样做,我会在以下行中遇到问题:“val quotesArray = parseResponse(response)”。唯一的区别是颠倒的,现在它将等待 JsonArray 并且响应是 givin JASONArray
    • 谢谢你,我和这个人打得很辛苦
    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多