json - 我无法在谷歌应用脚本中解析 JSON
问题描述
我正在尝试从 orion.apiseeds 歌词 API 中获取歌词 - 它可以正常工作,并且我得到了一个我无法解析的 JSON。
https://orion.apiseeds.com/api/music/lyric/Oasis/Wonderwall?apikey=xx 回馈
{"result":{"artist":{"name":"Oasis"},"track":{"name":"Wonderwall","text":"Today is gonna be the day that they're gonna throw it back to you.\r\nBy now you should've somehow realized what you gotta do.\r\nI don't believe that anybody, feels the way I do, about you now.\r\n\r\nBackbeat, the word is on the street that the fire in your heart is out.\r\nI'm sure you've heard it all before but you never really had a doubt.\r\nI don't believe that anybody, feels the way I do, about you now.\r\n\r\nAnd all the roads we have to walk are winding.\r\nAnd all the lights that lead us there are blinding.\r\nThere are many things that I, would like to say to you but I don't know how.\r\n\r\nBecause maybe, you're gonna be the one that saves me?\r\nAnd after all, you're my wonderwall.\r\n\r\nToday was gonna be the day but they'll never throw it back to you.\r\nBy now you should've somehow realized what you're not to do.\r\nI don't believe that anybody, feels the way I do, about you now.\r\n\r\nAnd all the roads that lead you there were winding.\r\nAnd all the lights that light the way are blinding.\r\nThere are many things that I, would like to say to you but I don't know how.\r\n\r\nI said maybe, you're gonna be the one that saves me?\r\nAnd after all, you're my wonderwall.\r\n\r\nI said maybe, (I said maybe)\r\nYou're gonna be the one that saves me?\r\nAnd after all, you're my wonderwall.\r\n\r\nI said maybe, (I said maybe)\r\nYou're gonna be the one that saves me? (that saves me)\r\nYou're gonna be the one that saves me? (that saves me)\r\nYou're gonna be the one that saves me? (that saves me)","lang":{"code":"en","name":"English"}},"copyright":{"notice":"Wonderwall lyrics are property and copyright of their owners. Commercial use is not allowed.","artist":"Copyright Oasis","text":"All lyrics provided for educational purposes and personal use only."},"probability":100,"similarity":1}}
我的代码是:
function getLyrics(url) {
var response = UrlFetchApp.fetch(url);
var responseObject = JSON.parse(response.getContentText());
}
然后,这就是问题出现的地方。
1) Logger.log(reponseObject); ==> 给出了我在上面粘贴的内容。
2)
for (var i=0;i<responseObject.length;i++) {
var item = responseObject[i];
Logger.log(JSON.stringify(item));
==> 在控制台中没有给出任何内容...
3)
function printJson(myObject) {
// define an array of all the object keys
var headerRow = Object.keys(myObject);
// define an array of all the object values
var row = headerRow.map(function(key){ return myObject[key]});
// define the contents of the range
var contents = [
headerRow,
row
];
// select the range and set its values
var ss = SpreadsheetApp.getActive();
var rng = ss.getActiveSheet().getRange(1, 1, contents.length, headerRow.length )
rng.setValues(contents)
}
===> 用“结果”填充一行,用所有其余的 JSON 填充下一行。
我觉得我错过了与 JSON 相关的基本内容......感谢您的帮助
解决方案
var responseObject = JSON.parse(response.getContentText());
var lyrics = responseObject.result.track.text;
这很容易;-)
推荐阅读
- laravel - 如何在生产服务器中设置 browsersync
- github - 我正在尝试返回 Github 中的特定提交以消除我犯的错误
- java - 将字母映射到电话键盘上的数字
- python-3.x - 如何使用 JWT 针对 Python Flask API 正确验证 Python Flask-WTF Web 应用程序用户
- python - 如何将度数转换为小时和分钟
- sql - SQL - 每行使用 CUBE 总计
- asp.net-core - 如何将一个项目中的 proto 文件包含在另一个项目中
- javascript - JavaScript 函数未从表单获取输入
- mongodb - 用于 Golang 的 Mongo DB 驱动程序上的 Mongo DB 查询
- javascript - Webpack-dev-server 很慢?