JSON字符串无法转换为array / ArrayList

时间:2019-07-04 14:53:17

标签: java android json gson

Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));            
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));       

c1.setTime(sdf.parse("20:00"));
System.out.println(c1.getTimeInMillis());

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

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

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

我陷入了将字符串转换为列表的问题。

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

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

以下代码不起作用。

转换代码1:

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" +
        "]";

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

转换代码2:

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

转换代码3

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

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

JsonString with codeRule online check error JsonString without codeRule online check error

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

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

只有我将jsonstring压缩为一个行,才可以将jsonstring转换为array / ArrayList。

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

有人知道

  • 第一季度:

如何处理codeRule?

  • 第二季度:

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

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

已更新:

产生一行json字符串:

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

粘贴的结果json字符串:

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

代码:

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);

1 个答案:

答案 0 :(得分:0)

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

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

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