0

我正在尝试将 JSON 数组转换为字符串,但它给了我错误,比如每次都删除这个令牌。我做错了什么吗?

private String jString = "{
    \"geodata\": [
        {
                \"id\": \"1\",
                \"name\": \"Jenifer Hathway\",
                \"email\": \"jh@gmail.com\",
                \"address\": \"xx-xx-xxxx,x - street, x - country\",
                \"gender\" : \"female\",
                \"latitude\" : \"37.33774833333334\",
                \"longitude\" : \"-121.88670166666667\",
                \"phone\": {
                    \"mobile\": \"+91 0000000000\",
                    \"home\": \"00 000000\",
                    \"office\": \"00 000000\"
                }
        },
        {
                \"id\": \"2\",
                \"name\": \"Johnny Depp\",
                \"email\": \"johnny_depp@gmail.com\",
                \"address\": \"xx-xx-xxxx,x - street, x - country\",
                \"gender\" : \"male\",
                \"latitude\" : \"37.336453\",
                \"longitude\" : \"-121.884985\",
                \"phone\": {
                    \"mobile\": \"+91 0000000000\",
                    \"home\": \"00 000000\",
                    \"office\": \"00 000000\"
                }
        }

    ]
}";
4

2 回答 2

3

Java 不支持多行字符串。

private String jString = "{"
+ "    \"geodata\": ["
+ "        {"
+ "                \"id\": \"1\","
+ "                \"name\": \"Jenifer Hathway\","
+ "                \"email\": \"jh@gmail.com\","
+ "                \"address\": \"xx-xx-xxxx,x - street, x - country\","
+ "                \"gender\" : \"female\","
+ "                \"latitude\" : \"37.33774833333334\","
+ "                \"longitude\" : \"-121.88670166666667\","
+ "                \"phone\": {"
+ "                    \"mobile\": \"+91 0000000000\","
+ "                    \"home\": \"00 000000\","
+ "                    \"office\": \"00 000000\""
+ "                }"
+ "        },"
+ "        {"
+ "                \"id\": \"2\","
+ "                \"name\": \"Johnny Depp\","
+ "                \"email\": \"johnny_depp@gmail.com\","
+ "                \"address\": \"xx-xx-xxxx,x - street, x - country\","
+ "                \"gender\" : \"male\","
+ "                \"latitude\" : \"37.336453\","
+ "                \"longitude\" : \"-121.884985\","
+ "                \"phone\": {"
+ "                    \"mobile\": \"+91 0000000000\","
+ "                    \"home\": \"00 000000\","
+ "                    \"office\": \"00 000000\""
+ "                }"
+ "        }"
+ "    ]"
+ "}";
于 2012-08-08T20:50:29.043 回答
0

您在此行有另一个错字

\"home\": \"00 000000",

应该

\"home\": \"00 000000\",

我也会尝试按照其他人的建议转义所有行

于 2012-08-08T20:52:33.597 回答