开发者

How to work with JSON URL

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开发者_Python百科 method calls on the URL to get the pics. Am I missing something? I am using Android to make this call.

Thanks, Matt


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;

public class Foo extends Activity
{
  @Override
  public void onCreate(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 = new JSONObject(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)
    {
      throw new RuntimeException(e);
    }
  }
}


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


why do the urls have escape characters if they are already strings?

Since:

  1. it is not uncommon to use JSON generating functions to produce JavaScript literals for embedding inside <script> elements
  2. HTML is often embedded in JSON and
  3. 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?


'/' characters must can 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜