将json放在String变量中

时间:2018-04-04 14:21:55

标签: java json

我想将所有Json内容放在String变量中,格式如下。

String inputJSON = "{
\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},
\"customerStatus\":\"\",
\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";

我是否知道为什么在完整的双引号中出现错误?我认为整个字符串是用双引号完成或覆盖的。我想把我的json放在这种格式中,请问我有什么遗漏或做错了吗?

3 个答案:

答案 0 :(得分:3)

您需要在每行的末尾添加String s = "abcdef"; String s = "abc"+"def"; String s = "abc"+ "def"; ,以便以有效的方式加入它们:

这三个是相同的:

String s =
    "{ "+
    "\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
    "\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
    "\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"},"+
    "\"customerStatus\":\"\","+
    "\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"}";

所以对你来说:

}

此外,您最后错过了{ "billingAddress": { "city": "", "country": "", "postalCode": "", "state": "", "streetAddress": "" }, "shippingAddress": { "city": "", "country": "", "postalCode": "", "state": "", "streetAddress": "" }, "personAddress": { "city": "", "country": "", "postalCode": "", "state": "", "streetAddress": "" }, "customerStatus": "", "createdDate": "pop" } 以获得有效的JSON字符串

如果你打印出上面的字符串,你会得到(一旦格式化)

private Entity e;

答案 1 :(得分:2)

遗憾的是,Java不支持多行String声明。您需要使用+来连接行

String inputJSON = "{"
    + "\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," 
    + "\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," 
    + "\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," 
    + "\"customerStatus\":\"\",\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";

答案 2 :(得分:1)

Java不支持多行字符串文字(尚未)。因此,每行必须是一个完整的字符串,用双引号括起来,每行需要使用' +'连接。

    final String inputJSON = "{" +
    "\"billingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
    "\"shippingAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
    "\"personAddress\":{\"city\":\"\",\"country\":\"\",\"postalCode\":\"\",\"state\":\"\",\"streetAddress\":\"\"}," +
    "\"customerStatus\":\"\"," +
    "\"createdDate\":\""+((String)globalMap.get("Json.Date"))+"\"";

也许更具可读性:

final String inputJSON =
    "{" +
    "    \"billingAddress\": {" +
    "        \"city\":\"\"," +
    "        \"country\":\"\"," +
    "        \"postalCode\":\"\"," +
    "        \"state\":\"\"," +
    "        \"streetAddress\":\"\"" +
    "    }," +
    "    \"shippingAddress\": {" +
    "        \"city\":\"\"," +
    "        \"country\":\"\"," +
    "        \"postalCode\":\"\"," +
    "        \"state\":\"\"," +
    "        \"streetAddress\":\"\"" +
    "    }," +
    "    \"personAddress\": {" +
    "        \"city\":\"\"," +
    "        \"country\":\"\"," +
    "        \"postalCode\":\"\"," +
    "        \"state\":\"\"," +
    "        \"streetAddress\":\"\"" +
    "    }," +
    "    \"customerStatus\":\"\"," +
    "    \"createdDate\": \"" + ((String)globalMap.get("Json.Date")) + "\"" +
    "}";

使用这种格式,很容易看到结束'}'也失踪了。

祝你好运。