简体   繁体   English

我无法在谷歌应用脚本中解析 JSON

[英]I don't manage to parse JSON in google app script

I'm trying to grab song lyrics from the orion.apiseeds lyrics API - it works and I get a JSON, that I'm incapable of parsing.我正在尝试从 orion.apiseeds 歌词 API 中获取歌词 - 它有效,我得到一个 JSON,我无法解析。

https://orion.apiseeds.com/api/music/lyric/Oasis/Wonderwall?apikey=xx gives back 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}}

My code to fetch this is:我的代码是:

function getLyrics(url) {
  var response = UrlFetchApp.fetch(url);
  var responseObject = JSON.parse(response.getContentText());
}

Then, that's where the problem arises.然后,这就是问题出现的地方。

1) Logger.log(reponseObject); 1) Logger.log(reponseObject); ==> gives the thing I pasted above. ==> 给出了我在上面粘贴的内容。

2) 2)

for (var i=0;i<responseObject.length;i++) {
    var item = responseObject[i];
    Logger.log(JSON.stringify(item));

==> gives NOTHING in the console... ==> 在控制台中没有给出任何内容...

3) 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) 
}

===> fills one row with "result" and the next row with all the rest of the JSON. ===> 用“结果”填充一行,用 JSON 的所有 rest 填充下一行。

I feel like i'm missing a basic JSON-related thing... Thanks for your help我觉得我错过了与 JSON 相关的基本内容......感谢您的帮助

var responseObject = JSON.parse(response.getContentText());
var lyrics = responseObject.result.track.text;

That was easy enough;-)这很容易;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google App Script 中解析 JSON 响应? - How to parse JSON response in Google App Script? 使用 App 脚本将嵌套的 Json 数据解析到 Google 表格 - Parse Nested Json Data to Google Sheets with App Script 我不知道如何解析这个嵌套的 json - I don't know how to parse this nested json 我使用 Google Apps 脚本创建了 Json 文件,我不想重写 URL,我想覆盖该文件 - I created a Json file with Google Apps Script, I don't want to rewrite the URL, I want to overwrite the file 用JSON解析Hashtable,完善(jsonencode)。 但是我不知道如何在Hashtable(jsondecode)中解析json。 (Android) - Parse Hashtable in JSON, perfect(jsonencode). But I don't know how to parse json in Hashtable(jsondecode). (Android) Google 应用程序脚本 JSON 解析 - Google apps script JSON parse 如何获取 JSON 数据并将其解析为 Google 表格脚本? - How do I fetch and parse JSON data to Google Sheets Script? 如何在Google App Engine中解析JSON? - How can I parse JSON in Google App Engine? 不明白如何解析json - Don't understand how to parse json 当我不知道键和属性没有名称时,如何在 C# 中解析 JSON 对象? - How do I parse a JSON object in C# when I don't know key and properties don't have names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM