Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

java - How to add a URL String in a JSON object

I need to add a URL typically in the format http:somewebsite.comsomepage.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...