简体   繁体   English

使用jQuery和jsonp通过键获取选定的JSON值(Vimeo API)

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

Let's say I have the following json result from an api: 假设我从api获得以下json结果:

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

Now let's say I have some jQuery code and I want to only retrieve title and url, but nothing else and do something with them like push them into the value of a textbox on my page or a hidden input. 现在让我们说我有一些jQuery代码,我只想检索标题和url,但别无其他,只能对它们进行处理,例如将其推入页面上的文本框的值或隐藏的输入中。 I have seen the $.each method that loops through them, but I don't wt to do that. 我已经看到$ .each方法遍历它们,但是我不愿意这样做。 I just want a few values and I want to get them by their key. 我只想要一些值,并希望通过它们的键来获取它们。

Here is the sample jQuery code that loops through them, I got from jQuery API docs. 这是我从jQuery API文档获得的示例jQuery代码。 How can I modify to just get those specific values? 如何修改以仅获取那些特定值?

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

From the JQuery.getJSON docs : 从JQuery.getJSON 文档

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. 成功回调将传递返回的数据,该数据通常是由JSON结构定义并使用$ .parseJSON()方法进行解析的JavaScript对象或数组。 It is also passed the text status of the response. 它还传递了响应的文本状态。

In your case its a Javascript object which will be accessible via property access 在您的情况下,它是一个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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM