【问题标题】:JSON string could not convert to array/ArrayListJSON 字符串无法转换为数组/ArrayList
【发布时间】:2019-07-04 14:53:17
【问题描述】:
[
    {
        countryCode: "CN",
        countryCallingCode: "+86",
        codeRule: "^1\d{10}$"
    },
    {
        countryCode: "US",
        countryCallingCode: "+1",
        codeRule: "^\d{10}$"
    }
]

所以我在 Kotlin 中这样定义模型

data class CountryCallingCode(
        val countryCode: String,
        val countryCallingCode: String,
        val codeRule: String? = null
)

这是后端文档定义响应的内容。 codeRule 是验证电话号码的正则表达式。

我坚持将字符串转换为列表。

我将它们粘贴到 Android Studio 中,显示如下:

String response = "[\n" +
        "    {\n" +
        "        countryCode: \"CN\",\n" +
        "        countryCallingCode: \"+86\",\n" +
        //"        codeRule: \"^1\\d{10}$\"\n" +
        "    },\n" +
        "    {\n" +
        "        countryCode: \"US\",\n" +
        "        countryCallingCode: \"+1\",\n" +
        //"        codeRule: \"^\\d{10}$\"\n" +
        "    }\n" +
        "]";

以下代码不起作用。

转换代码1:

Gson gson = new Gson()
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);

而且我认为以下代码是相同的,如果我错了,请纠正我。

转换代码2:

ArrayList<CountryCallingCode> countryCallingCodeList = (ArrayList<CountryCallingCode>)gson.fromJson(response, ArrayList.class);

转换代码 3

Gson gson = new Gson();
        Type type = new TypeToken<List<CountryCallingCode>>() {
        }.getType();
        List<CountryCallingCode> countryCallingCodeList = gson.fromJson(response, type);

然后我使用https://jsoneditoronline.org/ 重新格式化我的json。

我试图删除codeRule,并粘贴到Android Studio,它也告诉我我错了,它显示CountryCode导致SyntaxException。

    String reponse = "[\n" +
            "    {\n" +
            "        countryCode: \"CN\",\n" +
            "        countryCallingCode: \"+86\"\n" +
            "    },\n" +
            "    {\n" +
            "        countryCode: \"US\",\n" +
            "        countryCallingCode: \"+1\"\n" +
            "    }\n" +
            "]";

只有我将jsonstring压缩为oneline,我可以将jsonstring转换为array/ArrayList。

String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\"}]";

有谁知道

  • Q1:

codeRule怎么处理?

  • Q2:

为什么我不能将原始 JsonString 粘贴到 Android Studio?

为什么我必须将 JsonString 压缩成一行字符串?

更新:

原始一行json字符串:

[{"countryCode":"CN","countryCallingCode":"+86", codeRule: "^1\d{10}$"},{"countryCode":"US","countryCallingCode":"+1", codeRule: "^\d{10}$"}] 

粘贴的结果json字符串:

String response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\", codeRule: \"^1\\d{10}$\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\", codeRule: \"^\\d{10}$\"}]";

代码:

Gson gson = new Gson();

/* Convertion 1 */
CountryCallingCode[] countryCallingCodeList = gson.fromJson(response, CountryCallingCode[].class);

错误:

result = {JsonSyntaxException@7237} Method threw 'com.google.gson.JsonSyntaxException' exception.
 cause = {MalformedJsonException@7241} "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
 detailMessage = "com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 65 path $[0].codeRule"
 stackState = null
 stackTrace = {StackTraceElement[28]@7243} 
 suppressedExceptions = {Collections$EmptyList@7244}  size = 0
 shadow$_klass_ = {Class@1694} "class com.google.gson.JsonSyntaxException"
 shadow$_monitor_ = -2082115852

【问题讨论】:

  • A1:我认为这是解析正则表达式的问题related A2:直接粘贴时,android studio 添加了不应包含在 json 中的字符。它这样做是为了保留您实际上不想要的源格式。 JSON 格式没有空格,也没有\n,除非它们包含在字符串值中
  • 一个完全有效的 JSON,也将字段名称用引号括起来:[ { "countryCode":"CN", "countryCallingCode":"+86", "codeRule":"^1\\d{10}$" } ]
  • 我不知道为什么你得到codeRule 没有引号,但这是你的问题
  • @XtremeBaumer 谢谢,当响应为val response = "[{\"countryCode\":\"CN\",\"countryCallingCode\":\"+86\",\"codeRule\":\"^1\\\\d{10}$\"},{\"countryCode\":\"US\",\"countryCallingCode\":\"+1\",\"codeRule\":\"^\\\\d{10}$\"}]" 时,我可以运行并获取列表

标签: java android json gson


【解决方案1】:

这更像是一个评论而不是一个答案,但我需要更多的空间来解释 - 如果你的 Java 版本支持它,你可以尝试用这样的原始字符串创建你的响应:

String response = `[
    {
        countryCode: "CN",
        countryCallingCode: "+86",
        codeRule: "^1\d{10}$"
    },
    {
        countryCode: "US",
        countryCallingCode: "+1",
        codeRule: "^\d{10}$"
    }
]`

或者如果可能的话,使用 Kotlin,它使用 """ 来分隔原始字符串。它将更具可读性,并可能帮助您发现错误

【讨论】:

  • 如何用这样的原始字符串创建我的响应?我需要手动输入吗?
  • ' ' 对于 Java 和 Kotlin 都是不允许的,你能给我一个可构建的代码吗?
猜你喜欢
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 2017-04-10
  • 2020-03-24
  • 1970-01-01
相关资源
最近更新 更多