How to add a URL string to a JSON object

I need to add a URL, usually in the format http: \ someebsite.com \ somepage.asp. When I create a string with the above URL and add it to the json JSON object

using

json.put("url",urlstring); 

it adds an extra "\" and when I check the output as http:\\\\somewebsite.com\\somepage.asp

When I specify the URL as http://somewebsite.com/somepage.asp the json output is http:\/\/somewebsite.com\/somepage.asp

Can you help me get the url as is, please?

thanks

+6
source share
2 answers

Your JSON library automatically escapes characters such as slashes. On the receiving side, you will have to remove these backslashes with a function of type replace() .

Here is an example:

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

cleanedUrlString should be "http://somewebsite.com/somepage.asp" .

Hope this helps.

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

+6
source

Tichodroma's answer nailed it. You can solve the "problem" by keeping valid URLs.


In addition, the JSON format requires backslashes in strings to be escaped with a second backslash. If no second backslash is specified, the result is invalid JSON. See JSON Syntax Diagrams at http://www.json.org

The fact that a double backslash gives you problems actually means that the software that reads the files is broken. A correctly written JSON parser will automatically delete strings. The site I linked above lists many JSON parser libraries written in many languages. You should use one of them, and not try to write JSON parsing code yourself.

+3
source

Source: https://habr.com/ru/post/906477/


All Articles