使用 Java 正确编写 JSON 格式的 URL [英] Write correctly an URL in JSON with Java
问题描述
我需要在 JSON 文档上写一个属性,这个属性是一个 URL
I need to write an attribute on a JSON document, and this attribute is an URL
这是我的代码:
String url = "http://localhost:1028/accumulate";
JSONObject cabecera = new JSONObject();
cabecera.put("reference", url);
但是当我创建 JSON 时,这个属性是这样写的:
But when I create the JSON,this attibute is writted in this way:
"reference":"http:\/\/localhost:1028\/accumulate",
因此,接收 JSON 字符串的服务向我发送了一个错误:
So, the service that receives the JSON String, it's sending me an error:
<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 的正确写法是什么??
What is the correct way to write the URL??
提前致谢
推荐答案
但是当我创建 JSON 时,这个属性是这样写的:
But when I create the JSON,this attibute is writted in this way:
引用":http:\/\/localhost:1028\/accumulate",
没关系,反斜杠是无害的,无论您使用什么序列化为 JSON,它的转义都有些夸张.上面表示的字符串根本不包含反斜杠,只有斜杠.JSON 字符串中的 \/
与 /
完全相同,我们可以从 http://json.org:
That's fine, the backslashes are harmless, whatever you're using to serialize to JSON is just being a bit hyper with its escapes. The string represented by the above contains no backslashes at all, just slashes. \/
inside a JSON string is exactly the same as /
, as we can see from the highlighted rule from http://json.org:
(solidus"是斜线的奇特术语)
("solidus" is a fancy term for slash)
这篇关于使用 Java 正确编写 JSON 格式的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!