Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a URL String in a JSON object

Tags:

java

json

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

using

json.put("url",urlstring);

it's appending an extra "\" and when I check the output it's like http:\\\\somewebsite.com\\somepage.asp

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

Can you help me to retrieve the URL as it is, please?

Thanks

like image 760
VamsiKrishna Avatar asked Jan 20 '12 12:01

VamsiKrishna


People also ask

How do I add a URL to a JSON file?

Add a <a> tag with href and target="_black" for opening the link in new tab and use split to remove the href from json.

Can I use HTML tags in JSON?

It is possible to write an HTML string in JSON. You just need to escape your double-quotes.

What is URL encoded JSON?

Json url-encoder tool What is a json url-encoder? This tool converts JavaScript Object Notation (JSON) data to URL-encoding. All special URL characters get escaped to percent-number-number format. Json url-encoder examples Click to use. URL-escape a JSON Array.


1 Answers

Your JSON library automatically escapes characters like slashes. On the receiving end, you'll have to remove those backslashes by using a function like replace().

Here's 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.

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

like image 183
Simon Germain Avatar answered Sep 23 '22 07:09

Simon Germain