I've got a JSON object that looks something like this: (the following links are fake)
"results": {
"urlStuff": [
{"pic_url": "http:\/\/www.youtube.com\/inside\/kslkjfldkf\/234.jpg?v=7475646"},
{"other_pic_url": "http:\/\/www.youtube.com\/outside\/kslkjfldkf\/234.jpg?v=7475646"}
]
}
or something to that effect. My question is, why do the urls have escape characters if they are already strings? I am having to get rid of them to make the method calls on the URL to get the pics. Am I missing something? I am using Android to make this call.
Thanks, Matt
'/' characters
mustcan be escaped, as per JSON syntax: http://www.json.org/. Normally, whatever JSON API you are using should properly restore the escaped characters.Edit: Correction as per comments
They have escape characters because they are strings -- specifically, because they are JSON strings they have JSON string escape characters, and the entity that sent them to you decided to use the option to escape the solidus. For more information on why the sending entity may have made that choice, see the Why does the Groovy JSONBuilder escape slashes in URLs? post.
Take the easy route and just use a decent JSON parsing API to take care of automatically removing the JSON escape characters for you, when translating the JSON string into a Java String. Android has such a built-in JSON library available.
Since:
<script>
elements</
will terminate script blocks in HTML 4 (and</script>
will in all browsers)… escaping
/
characters ensures the data will be safe to drop into a<script>
element.Your JSON library should do that for you. Err … you are using a JSON library and not trying something crazy involving regular expressions, aren't you?
I couldn't see any escape character in the urls you provided but, nevertheless, URLs are encoded. I suggest you have a look at URLEncoder. This class offers different ways to encode a URL.
Normally the standard implies that URLs are encoded using UTF-8. But, for some languages, the encoding and charset can different. Recently I had to deal with urls containing asian characters and they were encoded in other charsets (namely eur-ko for Korean, for instance).
I used this site to decode/encode maually a few urls and find out the charset.
Once you found the right charsets to use, you can use the Charset class of the Java sdk to transform urls into normal utf-16 java string. Tutorial here.
Regards, Stéphane