使用Java在JSON中正确编写URL

jjm*_*nez 1 java string url json

我需要在JSON文档上写一个属性,这个属性是一个URL

这是我的代码:

String url = "http://localhost:1028/accumulate";
JSONObject cabecera = new JSONObject();
cabecera.put("reference", url);
Run Code Online (Sandbox Code Playgroud)

但是当我创建JSON时,这个属性就是以这种方式编写的:

"reference":"http:\/\/localhost:1028\/accumulate",
Run Code Online (Sandbox Code Playgroud)

所以,接收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>
Run Code Online (Sandbox Code Playgroud)

写URL的正确方法是什么?

提前致谢

T.J*_*der 5

但是当我创建JSON时,这个属性就是以这种方式编写的:

"reference":"http:\/\/localhost:1028\/accumulate",

没关系,反斜杠是无害的,无论你使用什么来序列化到JSON只是有点超级逃脱.上面表示的字符串根本不包含反斜杠,只是斜杠.\/在JSON字符串中/,与我们从http://json.org中突出显示的规则中可以看到的完全相同:

在此输入图像描述

("solidus"是斜线的奇特术语)