簡體   English   中英

我無法在谷歌應用腳本中解析 JSON

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

我正在嘗試從 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 的所有 rest 填充下一行。

我覺得我錯過了與 JSON 相關的基本內容......感謝您的幫助

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

這很容易;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM