javascript - 如何获取图片地址

标签 javascript json twitter nodes

如果我使用以下方法从 Twitter API 解析推文:

  tweet.entities.media

我得到以下 json。但是我将如何访问 media_url 属性?它嵌套在 json 对象中一个非常困难的地方。

 tweet.entities.media[0].media_url

返回错误。

{
  "image": [
    {
      "id": 511403875438301200,
      "id_str": "511403875438301185",
      "indices": [
        44,
        66
      ],
      "media_url": "http://some_url.jpg",
      "media_url_https" : "https: //some_url.jpg",
      "url": "http://whatever.com",
      "display_url": "pic.twitter.com/BNIPh3ZlRD",
      "expanded_url": "https://twitter.com/some_url/1",
      "type": "photo",
      "sizes": {
        "thumb": {
          "w": 150,
          "h": 150,
          "resize": "crop"
        },
        "small": {
          "w": 680,
          "h": 482,
          "resize": "fit"
        },
        "large": {
          "w": 2048,
          "h": 1453,
          "resize": "fit"
        },
        "medium": {
          "w": 1200,
          "h": 851,
          "resize": "fit"
        }
      }
    }
  ]
}

最佳答案

how would I access media_url?

在我看来,您只是缺少 image[0]:

tweet.entities.media.image[0].media_url

$(document).ready(function() {
var tweet = jQuery.parseJSON( '{"entities":{"media":{"image":[{"id":511403875438301200,"id_str":"511403875438301185","indices":[44,66],"media_url":"http://pbs.twimg.com/media/BxjfHXxCEAETmig.jpg","media_url_https":"https: //pbs.twimg.com/media/BxjfHXxCEAETmig.jpg","url":"http://whatever.com","display_url":"pic.twitter.com/BNIPh3ZlRD","expanded_url":"https://twitter.com/AWish4Me/status/511403876642463744/photo/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":482,"resize":"fit"},"large":{"w":2048,"h":1453,"resize":"fit"},"medium":{"w":1200,"h":851,"resize":"fit"}}}]}}}');
document.body.innerHTML = tweet.entities.media.image[0].media_url;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于javascript - 如何获取图片地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724929/

相关文章:

javascript - 范围问题

JavaScript:暂停函数并等待全局变量

java - GSON-JSON反序列化类中的对象

json - 如何将 utf8 字符串转换为 []byte?

twitter - 使用内部ID而不是屏幕名称链接到Twitter个人资料

javascript - tty.js 不工作

javascript - 通过 Response.ContentType、Response.End 输出文件时如何显示进度状态/微调器?

php - 使用自定义 JSON 模式返回分页的 Eloquent 模型?

ios - 如何使用 JSONDecoder 获取推文

python-2.7 - 使用 Python Tweepy 在 Twitter API 上发出非常具体的时间请求(到第二个)?