-2

I am trying to get the value from a json array returned by the bit.ly API

The response is:

{"status_code": 200, "data": {"bundle": {"og_image": null, "bundle_owner": "user", "created_ts": 1423596727, "description": "", "links": [{"updated_by": null, "title": "", "display_order": 0, "lm": 1423597992.378417, "ts": 1423597992.378417, "comments": [], "aggregate_link": "http://bit.ly/LmvF", "long_url": "http://google.com/", "link": "http://bit.ly/1AUeg5k", "preview": 1, "added_by": "rentthesun"}], "title": "Barcelona 226", "collab": [], "private": true, "inv_collab": [], "last_modified_ts": 1423597992, "bundle_link": "http://bitly.com/bundles/myuser/2", "preview": true}}, "status_txt": "OK"}

I want to get the value of "link"

My code:

$.getJSON("https://api-ssl.bitly.com/v3/bundle/link_add?", {
     "access_token": "MY_TOKEN",
      "bundle_link": "https://bitly.com/bundles/user/2",
      "link": raw_url,
 }, function(response) {
       $("#link-container").show("fast");
       link = "<a href=\"" + response.data['bundle']['links'].link[0] + "\" target=\"_blank\">" + response['data']['bundle']['links'].link[0] + "</a>";
       $("#link").html(link);
 });

How can i get the shortened link?

Josh Fradley
  • 545
  • 1
  • 10
  • 23

1 Answers1

0
link = "<a href=\"" + response.data['bundle']['links'].link[0] + "\" target=\"_blank\">" + response['data']['bundle']['links'].link[0] + "</a>";

I think you mean response['data']['bundle']['links'][0].link. This will select the link of the first element, while your original code was looking for the first element of a link array.

And if you'll always need it to be the last link, it's easiest to save the links array, and get the last element of it:

links = response.data['bundle']['links'];
link = "<a href=\"" + links[links.length-1].link + "\" target=\"_blank\">" + links[links.length-1].link + "</a>";
Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • this works for the first time the button is clicked but not again, as it becomes link[1]. How can i make it get the most recent one? – Josh Fradley Feb 10 '15 at 21:11