一个衬垫将json加载到变量中

时间:2014-02-24 01:53:28

标签: javascript json google-apps-script

我想将此JSON加载到变量:http://itunes.apple.com/lookup?id=327630330

是否有快速而快速的方法不需要外部库?

2 个答案:

答案 0 :(得分:3)

看起来该服务支持JSONP,因此您只需定义一个函数并包含指向该服务的脚本标记:

<script>
function iTunesData(data) {
    // do something with data
}
</script>
<script src="http://itunes.apple.com/lookup?id=327630330&callback=iTunesData"></script>

当然假设您在浏览器上运行代码。 You can also create the script element dynamically

答案 1 :(得分:0)

我的环境是Google Spreadsheets。 我发现了如何在这种环境下做到这一点:

var result = UrlFetchApp.fetch("http://someurl/mydata.json");
var o  = Utilities.jsonParse(result.getContentText());

参考此处:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

FWIW我最终编写了这个自定义函数,以便通过Apple ID从iTunes获取项目的标题(app,song等)。您可以在Google Spreadsheets中使用它:

function jghgTitleForAppleid(appleid) {
  var theid = appleid;
  var completeurl = 'http://itunes.apple.com/lookup?&id=' + theid;

  response = UrlFetchApp.fetch(completeurl);
  var o = Utilities.jsonParse(response.getContentText());
  var title = o["results"][0]["trackName"]

  return title;
}
相关问题