如何使用 JSON URL

发布于 2024-11-18 18:37:37 字数 401 浏览 9 评论 0原文

我有一个 JSON 对象,看起来像这样:(以下链接是假的)

"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"}
    ]
}

或类似的东西。我的问题是,如果 URL 已经是字符串,为什么还要包含转义字符?我必须摆脱它们才能在 URL 上调用方法来获取图片。我错过了什么吗?我正在使用 Android 来拨打此电话。

谢谢, 马特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风苍溪 2024-11-25 18:37:37

如果 URL 已经是字符串,为什么还要包含转义字符?

它们具有转义字符因为它们是字符串 - 具体来说,因为它们是 JSON 字符串,所以它们具有 JSON 字符串转义字符,并且将它们发送给您的实体决定使用该选项来转义斜线。有关发送实体为何做出此选择的更多信息,请参阅 为什么 Groovy JSONBuilder 对 URL 中的斜杠进行转义? 帖子。

我必须摆脱它们才能在 URL 上调用方法来获取图片。我错过了什么吗?

在将 JSON 字符串转换为 Java 字符串时,采用简单的方法,只需使用合适的 JSON 解析 API 即可自动删除 JSON 转义字符。 Android 有这样一个内置的 JSON 库可用。

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);
    }
  }
}

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);
    }
  }
}
缺⑴份安定 2024-11-25 18:37:37

我在您提供的网址中看不到任何转义字符,但尽管如此,网址还是经过编码的。我建议您查看 URLEncoder。此类提供了对 URL 进行编码的不同方法。

通常,该标准意味着 URL 使用 UTF-8 进行编码。但是,对于某些语言,编码和字符集可能不同。最近,我必须处理包含亚洲字符的网址,并且它们是用其他字符集编码的(例如,韩语的 eur-ko)。

我使用这个网站手动解码/编码一些网址并找出字符集。

一旦找到合适的字符集,您就可以使用 Java sdk 的 Charset 类将 url 转换为正常的 utf-16 java 字符串。 教程在这里

问候,
史蒂芬

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

兔小萌 2024-11-25 18:37:37

如果 URL 已经是字符串,为什么还要包含转义字符?

因为:

  1. 使用 JSON 生成函数生成 JavaScript 文本以嵌入
  2. HTML 通常嵌入在 JSON 中,并且
  3. 序列 将终止 HTML 4 中的脚本块(并且 将在所有浏览器中)

...转义 / 字符确保数据可以安全地放入

我必须摆脱它们才能在 URL 上调用方法来获取图片。

您的 JSON 库应该为您做到这一点。呃……您正在使用 JSON 库,而不是尝试涉及正则表达式的疯狂操作,不是吗?

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?

看春风乍起 2024-11-25 18:37:37

'/' 字符必须可以被转义,按照 JSON 语法: http://www.json.org/。通常,无论您使用什么 JSON API 都应该正确恢复转义字符。

编辑:根据评论更正

'/' 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文