我需要在一个JSON文档上写一个属性,这个属性是一个URL
这是我的代码:
String url = "http://localhost:1028/accumulate";
JSONObject cabecera = new JSONObject();
cabecera.put("reference", url);
但是当我创建JSON时,这个属性是这样写的:
"reference":"http:\/\/localhost:1028\/accumulate",
因此,接收 JSON 字符串的服务向我发送错误:
<subscribeContextResponse>
<subscribeError>
<errorCode>
<code>400</code>
<reasonPhrase>Bad Request</reasonPhrase>
<details>JSON Parse Error: <unspecified file>(1): invalid escape sequence</details>
</errorCode>
</subscribeError>
</subscribeContextResponse>
URL 的正确写法是什么??
提前致谢
最佳答案
But when I create the JSON,this attibute is writted in this way:
"reference":"http:\/\/localhost:1028\/accumulate",
没关系,反斜杠是无害的,无论您用来序列化为 JSON 的是什么,它的转义都只是有点过度。上面表示的字符串根本不包含反斜杠,只有斜杠。 JSON 字符串中的\/
与/
完全相同,我们可以从http://json.org 中突出显示的规则中看出这一点。 :
(“solidus”是斜线的别称)
关于java - 使用 Java 正确写入 JSON 中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27447101/