当有更多具有相同名称的数据对象时,如何仅从JSON响应中获取第一个数据对象的值?

时间:2015-11-30 13:40:05

标签: javascript json ajax api url

我希望将JSON请求中的值填入源URL,如下面的代码所示:

function Source() {
$.ajax({
    type: "GET",
    dataType: "json",
    url: "https://api.hitbox.tv/media/video/chairstream/list?filter=recent",
    success: function(data) {
        var idvid = data.media_id;
        document.getElementById("latest").src = "http://www.hitbox.tv/#!/embedvideo/" + idvid() + "?autoplay=true";
    }
});

问题是变量idvid没有在网址中完成他的工作。 另一个问题是JSON响应中有更多名为"media_id"的数据对象,我不知道如何只获取第一个media_id的值。当两个问题都能解决时,我会非常高兴。

2 个答案:

答案 0 :(得分:2)

不确定为什么在idvid之后你有大括号,但它应该只是

"http://www.hitbox.tv/#!/embedvideo/" + idvid + "?autoplay=true";

获取media_id数组中第一个对象的data

var idvid = data.video[0].media_id;

答案 1 :(得分:1)

我调用了网址并使用此

获取了该网址
data.video[0].media_id

所以(如安迪所建议的那样从var中删除()):

success: function(data) {
    $("#latest").attr("src","http://www.hitbox.tv/#!/embedvideo/" + 
     data.video[0].media_id + 
     "?autoplay=true");
}



var data = {
  "request": {
    "this": "\/media\/video\/chairstream\/list"
  },
  "media_type": "video",
  "video": [{
    "media_user_name": "chairstream",
    "media_id": "783994",
    "media_file": "2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938",
    "media_user_id": "871797",
    "media_profiles": "[{\"url\":\"\\\/chairstream\\\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938\\\/chairstream\\\/index.m3u8\",\"height\":\"800\",\"bitrate\":0}]",
    "media_type_id": "2",
    "media_is_live": "1",
    "media_live_delay": "0",
    "media_date_added": "2015-11-27 23:16:04",
    "media_live_since": null,
    "media_transcoding": null,
    "media_chat_enabled": "1",
    "media_countries": null,
    "media_hosted_id": null,
    "media_mature": null,
    "media_hidden": null,
    "media_offline_id": null,
    "user_banned": null,
    "media_name": "2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938",
    "media_display_name": "chairstream",
    "media_status": "Chairstream.com - Nov 27th #5",
    "media_title": "Chairstream.com - Nov 27th #5",
    "media_description": "",
    "media_description_md": null,
    "media_tags": "",
    "media_duration": "6262.0000",
    "media_bg_image": null,
    "media_views": "9",
    "media_views_daily": "2",
    "media_views_weekly": "9",
    "media_views_monthly": "9",
    "category_id": null,
    "category_name": null,
    "category_name_short": null,
    "category_seo_key": null,
    "category_viewers": null,
    "category_media_count": null,
    "category_channels": null,
    "category_logo_small": null,
    "category_logo_large": null,
    "category_updated": null,
    "team_name": null,
    "media_start_in_sec": "0",
    "media_duration_format": "01:44:22",
    "media_thumbnail": "\/static\/img\/media\/videos\/2c7\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938_mid_000.jpg",
    "media_thumbnail_large": "\/static\/img\/media\/videos\/2c7\/2c767c827c6c84a4714b9379983de69449c5a1c1-5658c80c2b938_large_000.jpg",
    "channel": {
      "followers": "0",
      "user_id": "871797",
      "user_name": "chairstream",
      "user_status": "1",
      "user_logo": "\/static\/img\/channel\/chairstream_5602c1d59d02e_large.png",
      "user_cover": "\/static\/img\/channel\/cover_53e89056cf3ef.jpg",
      "user_logo_small": "\/static\/img\/channel\/chairstream_5602c1d59d02e_small.png",
      "user_partner": null,
      "partner_type": null,
      "user_beta_profile": "0",
      "media_is_live": "0",
      "media_live_since": "2015-11-30 12:35:33",
      "user_media_id": "206125",
      "twitter_account": null,
      "twitter_enabled": null,
      "livestream_count": "1"
    }
  }]
}

console.log(data.video[0].media_id)