Jakea Hacks Jakea Hacks - 17 days ago 6
Javascript Question

Get value from JSON API in Javascript or Jquery

I have a JSON API

{
"code": 1000,
"data": {
"slider_infinite": [
{
"link": "rules",
"order": 0,
"picURL": "http://pic.api.vshare.com/dd/f1/66/1c/9e455664a0f1453ef1a56e6d00f0e664.png",
"title": "金币"
}
],
"InfiniteApp": [
{
"sort": 14,
"category": "Infinite gold",
"adslink_url": "",
"limit_free": 0,
"trackName": "The Sims™ 免费版",
"icon": "http://is4.mzstatic.com/image/thumb/Purple71/v4/b6/9b/fa/b69bfa28-5190-8ff3-99c5-db11004e6e15/source/114x114bb.jpg",
"point": 0,
"trackId": 496009190,
"averageUserRating": 4,
"ipaDownloadUrl": "http://hot.haima.me/infinite/201611/1618/com.ea.sims3deluxe.ipa",
"seo_name": "",
"infiniteGold": 1,
"shortVersion": "5.22.2",
"downloadCount": 81504,
"app_type": "",
"isFromAppstore": 0,
"size": "199 MB",
"ID": "496009190",
"ipaVersion": "5.22.2",
"bundleId": "com.ea.sims3deluxe.ipad.bv.c"
}
]
},
"time": 0.014687061309814453
}


Now I want to get the
ipaDownloadUrl
value and put that into a variable using javascript or Jquery

Don't mark as duplicate. I have tried all the possible ways and no one worked. Also, I am new here and don't know so much stuff about JSON

Answer

You can try

var myUrl = '';
$.ajax({
  url: 'yourdomain.com',
  type: 'GET',
  contentType: 'application/json',
  success: function(response) {
    myUrl = response.data.InfiniteApp[0].ipaDownloadUrl;
  }
});

The ajax call is asynchronous, so it may take some time for myUrl to be ready for use outside of it. I would recommend to do what you need to do with myUrl inside the success function, or use promises.

Comments