How To Work With Json Url
Solution 1:
why do the urls have escape characters if they are already strings?
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.
I am having to get rid of them to make the method calls on the URL to get the pics. Am I missing something?
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.
package com.stackoverflow.q6564078;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
publicclassFooextendsActivity
{
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// {"pic_url": "http:\/\/www.youtube.com\/inside\/kslkjfldkf\/234.jpg?v=7475646"}String jsonInput = "{\"pic_url\": \"http:\\/\\/www.youtube.com\\/inside\\/kslkjfldkf\\/234.jpg?v=7475646\"}";
Log.d("JSON INPUT", jsonInput);
// output: {"pic_url": "http:\/\/www.youtube.com\/inside\/kslkjfldkf\/234.jpg?v=7475646"}try
{
JSONObject jsonObject = newJSONObject(jsonInput);
String javaUrlString = jsonObject.getString("pic_url");
Log.d("JAVA URL STRING", javaUrlString);
// output: http://www.youtube.com/inside/kslkjfldkf/234.jpg?v=7475646
}
catch (Exception e)
{
thrownewRuntimeException(e);
}
}
}
Solution 2:
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
Solution 3:
why do the urls have escape characters if they are already strings?
Since:
- it is not uncommon to use JSON generating functions to produce JavaScript literals for embedding inside
<script>
elements - HTML is often embedded in JSON and
- The sequence
</
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.
I am having to get rid of them to make the method calls on the URL to get the pics.
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?
Solution 4:
'/' 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
Post a Comment for "How To Work With Json Url"