将JSON数组表示为String

时间:2012-08-08 20:42:13

标签: java json parsing

我正在尝试将一个JSON数组放入String中,但是它给了我错误,就像每次删除此标记一样。我在做什么事吗?

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\"
                }
        }

    ]
}";

2 个答案:

答案 0 :(得分: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\""
+ "                }"
+ "        }"
+ "    ]"
+ "}";

答案 1 :(得分:0)

你在这一行还有另一个错字

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

应该是

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

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