how do I convert a string url = "http://abcdef.com/"
to JSONObject having a field "url" ="http://abcdef.com/"
? The result I am getting is : "url" = "http:\/\/abcdef.com"
.So, I am not able to get around '/'

- 42,730
- 18
- 77
- 103

- 31
- 1
- 4
-
Are you saying you have a problem with the escaped slashes? It's completely fine to have them escaped in a JSON string. Do you have a concrete problem with them? – RoToRa Dec 23 '10 at 13:56
-
I have to use to a JSON object having the url in the form "http://abcdef.com" without the escaped backslashes in my next module so, that is it – wishy Dec 23 '10 at 14:01
-
1For the record (as per my deleted answer because the question was initially unclear), OP is using [json-simple](http://code.google.com/p/json-simple/) and want to prevent JS-escaping. – BalusC Dec 23 '10 at 14:10
-
You are aware that the backslashes are only needed in the JSON repesentation? The actual string doesn't contain any backslashes. – RoToRa Dec 23 '10 at 15:53
-
"backslashes are only needed in the JSON" -- no, they are not *needed*, they are being escaped defensively. See http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped – Jason S Dec 24 '10 at 02:59
1 Answers
If that's the way your JSONObject serializes strings (which is legal as per the JSON spec), there's no way to get around it directly unless you alter the behavior of the JSONObject. (by rewriting, subclassing, or wrapping -- see below.)
Indirectly, if you have an opportunity to intercept the JSONObject output, convert to string, and replace all instances of \/
with /
.
update: I'm not familiar with the specific implementation you're using, but if you have existing code that expects a JSONObject and takes an interface rather than a particular class implementation, you could write a decorator or wrapper class that holds an instance to the "real" JSONObject class, and delegates everything to the JSONObject, but fixes up the conversion of strings so it converts \/
to /
after-the-fact automatically.

- 184,598
- 164
- 608
- 970
-
sry man, new here.. :) Exactly what I was trying to do in the meantime :). So what I do is I create a JSONObject with url field as "http:\/\/abcdef.com" Now to test if can be used without escaped backslshes, I parse it using JSONParser and then extract the url field of the JSONObject I created. It then returns it to me in the correct format(which it should have and hence can be used in the next module).. thanx all :) – wishy Dec 23 '10 at 14:38