【问题标题】:Getting Gson MalformedJsonException: Unterminated array获取 Gson MalformedJsonException:未终止的数组
【发布时间】:2018-05-25 21:49:01
【问题描述】:

我正在创建一个自定义键盘应用程序。在onCreateInputView 方法中,我在协程中包含以下代码,该协程解析来自https://github.com/vedantroy/image-test/raw/master/index.json 的文件(使用Gson 库中的JsonParser)。

Log.d("VED-APP","Parsing File From Github")
JsonParser().parse(fetchString("https://github.com/vedantroy/image-test/raw/master/index.json"))

其中fetchString() 是以下方法:

private suspend fun fetchString(url: String): String = suspendCoroutine { cont ->
    url.httpGet().header(Pair("pragma", "no-cache"), Pair("cache-control", "no-cache")).responseString { _, _, result ->
        //Log.d("VED-APP", r.toString())

        when (result) {
            is Result.Failure -> {
                cont.resumeWithException(result.getException())
            }
            is Result.Success -> {
                cont.resume(result.value)
            }
        }

    }
}

上面的代码运行良好,没有任何错误。我可以使用 Gson 成功解析位于 URL 的文件。

但是,当我尝试使用以下代码从本地源而不是 github 解析文件(具有相同的确切内容)时,出现异常。

val layoutFileName = "index.json"
val layoutFile =  File(filesDir, layoutFileName)


        //If layout file does not exist, create it and write default layout to file.
        if(!layoutFile.exists()) {
            Log.d("VED-APP","FILE DOES NOT EXIST")
            layoutFile.writeText(resources.getString(R.string.default_keyboard_layout))
        } else {
            Log.d("VED-APP","FILE EXISTS")

        }

        Log.d("VED-APP","Parsing Local File")
       JsonParser().parse(FileReader(layoutFile))

例外是:

com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:未终止的数组在第 1 行第 18 列路径 $.Excited1

这是strings.xml中的条目default_keyboard_layout的内容:

这很奇怪,因为strings.xml 中的default_keyboard_layout 条目的内容与github 上的index.json 文件的内容完全相同。

我做了一些调试,我想我找到了错误的根源。 我将此添加到我的代码中,以便在解析之前查看index.json 的内容

layoutFile.forEachLine {
            Log.d("VED-APP",it)
        }

我得到了以下结果:

 { Excited:[https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif], Sad:[https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif] }

这个输出有两点很奇怪。

1)虽然strings.xml中的条目和github上的文件都是多行的,但一切都在一行中

2) 尽管strings.xml中的条目和github上的文件有引号,但文件没有引号。

我怀疑这两个问题是问题的根源,但我不确定。

为什么会出现此异常以及如何解决?

更新 1 -

我尝试了 Tynn 的解决方案,也更改了我的 strings.xml

<resources>
    <string name="app_name">Anime Face Keyboard</string>
    <string name="subtype_en_US">English (US)</string>
    <string name="default_keyboard_layout"><![CDATA[{
    "Excited":["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
                "https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
                "https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"],

    "Sad":["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"]
}]]></string>
</resources>

但这没有任何作用。

我打印了resources.getString(R.string.default_keyboard_layout) 的值,但它仍然缺少引号和换行符。

【问题讨论】:

    标签: android json kotlin gson


    【解决方案1】:

    您必须将" 转义为\",并将换行符转义为\n

    由于字符串是在 XML 中定义的,因此换行符被视为标记的一部分," 用于定义包含标记的字符串,例如空格-break 在行尾:

    <string name="a">a </string>
    <string name="b">"b "</string>
    

    这里a 只是"a"b"b "

    <string>"{\n
        \"key\": [\"value\"]\n
    }\n"</string>
    

    【讨论】:

    • 嗯....那行不通。我用我的解决方案实施更新了我的帖子。
    • 对不起。无论如何,考虑转义字符。
    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2023-04-03
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2020-02-21
    相关资源
    最近更新 更多