将内容转换为文本解析 JSON URL 响应

Convert content to text parsing JSON URL response

提问人:mika_972 提问时间:11/2/2023 更新时间:11/2/2023 访问量:36

问:

我有一个网址,结果如下所示

    "playerInfo": {
        "059gh": {
            "eligiblePos": "Flx3,G,Flx,Flx2,PG",
            "status": "FA"
        },
        "04mr6": {
            "eligiblePos": "Flx3,G,Flx,Flx2,PG",
            "status": "FA"
        },
        "059fz": {
            "eligiblePos": "Flx3,G,Flx,Flx2,PG",
            "status": "FA"
        },
        "059fw": {
            "eligiblePos": "Flx3,G,Flx,Flx2,PG",
            "status": "FA"
        },
        "033b7": {
            "eligiblePos": "Flx3,Flx,Flx2,C",
            "status": "FA"
        },
        ...

我有一个函数可以尝试检索玩家 ID 和 eligiblePos 例如:059GH // Flx3,G,Flx,Flx2,PG

function getPlayerData() {
  var DATA_SPREADSHEET_ID = "xxxx"
  var ss = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
  var PlayerDataSheet = ss.getSheetByName("PlayerData");
  PlayerDataSheet.clearContents();
  var url = 'https://www.fantrax.com/fxea/general/getLeagueInfo?leagueId=' + leagueID;
  var response = UrlFetchApp.fetch(url);
  var content = response.getContentText();
  var data = JSON.parse(content);
  var playerKeys = Object.keys(data.playerInfo);
  PlayerDataSheet.appendRow(playerKeys)
  ...

我遇到的问题是我的电子表格中 playerKeys 的结果,选项卡“PlayerData”,显示为数字,我想转换为文本。

事实上,033b7 被转换为 33b7 或 03E75 至 3E+75

我试图投射到文本中,但没有成功

json 对象 google-apps-script google-sheets fetch

评论

0赞 TheMaster 11/2/2023
尝试在数据前加上 a:like''033b7

答:

0赞 Cooper 11/2/2023 #1

尝试:

function myfunk() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0")
  let data = {playerInfo: {"059gh": { "eligiblePos": "Flx3,G,Flx,Flx2,PG", "status": "FA" },"04mr6": { "eligiblePos": "Flx3,G,Flx,Flx2,PG", "status": "FA" },"059fz": { "eligiblePos": "Flx3,G,Flx,Flx2,PG", "status": "FA" },"059fw": { "eligiblePos": "Flx3,G,Flx,Flx2,PG",  "status": "FA"},"033b7": { "eligiblePos": "Flx3,Flx,Flx2,C","status": "FA"}}};
  var playerKeys = Object.keys(data.playerInfo);
  sh.getRange(sh.getLastRow() + 1, 1, 1, playerKeys.length).setValues([playerKeys]).setNumberFormat("0.###############")
}
0赞 mika_972 11/2/2023 #2

谢谢 我为解决这个问题做了什么, 是创建一个列表(在单元格 A1 中),其中“|”作为分隔符

  var listplayer = playerKeys.join("|");
  PlayerDataSheet.getRange("A1").setValue(listplayer);

然后感谢那个页面:拆分谷歌表格保留字符, 我直接将结果拆分到另一个工作表中,保留原始文本

=TRANSPOSE(SPLIT(REGEXREPLACE(A1,"^|\|","|'"),"|"))