HOW TO ADD A URL STRING IN A JSON OBJECT

Linda Miller

Total Post:2

Points:14
Posted by  Linda Miller
Java  JSON 
 1067  View(s)
Ratings:
Rate this:
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?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: How to add a URL String in a JSON object

    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".

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!