如何在JSON对象中添加URL字符串

Vam*_*hna 9 java json

我需要通常以http:\ somewebsite.com\somepage.asp格式添加URL.当我使用上面的URL创建一个字符串并将其添加到JSON对象json时

运用

json.put("url",urlstring);
Run Code Online (Sandbox Code Playgroud)

它附加了一个额外的"\",当我检查输出时就像是 http:\\\\somewebsite.com\\somepage.asp

当我给出http://somewebsite.com/somepage.asp json输出的URL时http:\/\/somewebsite.com\/somepage.asp

你能帮我检查一下这个URL吗?

谢谢

Sim*_*ain 8

您的JSON库会自动转义斜杠等字符.在接收端,你必须使用像这样的函数删除这些反斜杠replace().

这是一个例子:

string receivedUrlString = "http:\/\/somewebsite.com\/somepage.asp";<br />
string cleanedUrlString  = receivedUrlString.replace('\', '');
Run Code Online (Sandbox Code Playgroud)

cleanedUrlString应该是"http://somewebsite.com/somepage.asp".

希望这可以帮助.

参考:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char,%20char)