使用 jQuery 和 jsonp 按键获取选定的 json 值(vimeo api)

Get selected json value by key using jQuery and jsonp (vimeo api)

假设我有以下 json 来自 api 的结果:

{
  id: 116213129,
  title: "“An Episode,” by Palaxy Tracks",
  description: "Music: Palaxy Tracks<br /> Animation: Luca Tóth and Stephen McNally",
  url: "http://vimeo.com/116213129",
  thumbnail_small: "http://i.vimeocdn.com/video/502490003_100x75.jpg",
  thumbnail_medium: "http://i.vimeocdn.com/video/502490003_200x150.jpg",
  thumbnail_large: "http://i.vimeocdn.com/video/502490003_640.jpg",
  user_id: 14252792,
  width: 1920,
  height: 1080,
  tags: "palaxy tracks, music, an episode, animation, music video"
}

现在假设我有一些 jQuery 代码,我只想检索标题和 url,但不想对它们做一些事情,比如将它们推入我的文本框的值页面或隐藏的输入。我见过循环遍历它们的 $.each 方法,但我不想那样做。我只想要一些值,我想通过它们的键来获取它们。

这是循环遍历它们的示例 jQuery 代码,我从 jQuery API 文档中获得。我如何修改才能获得这些特定值?

<script>
  $.getJSON("http://vimeo.com/api/v2/video/116213129.json?jsoncallback=?", function (result) {
    var title = result.title;
    var description = result.description;
    $("#popVals").click(function () {
      $('#Video_Title').val(title);
      $('#Video_Description').val(description);
    });
  });
</script>

来自JQuery.getJSON docs:

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

在您的情况下,它是一个 Javascript 对象,可以通过 属性 访问

访问
<script>
  $.getJSON("http://vimeo.com/api/v2/video/116213129.json?jsoncallback=?", function (result) {
    var title = result.title;
    var url = result.url;
    //do something

  });
</script>