首页 > 解决方案 > 使用 JSON.parse 从魔兽日志 API 获取数据

问题描述

我是 javascript 的新手,并试图从魔兽日志中获取这个角色的解析到谷歌表格上的电子表格中。到目前为止,我有这个,但它一直在记录 Null 值。

function test() {
  

  var response =
  UrlFetchApp.fetch("https://www.warcraftlogs.com/v1/parses/character/Fistweaverz/Illidan/US? 
  api_key=xxxxx");

  
  var json = response.getContentText();
  var data = JSON.parse(json);

  Logger.log(data["percentile"]);
}

对象“数据”的日志:

 [20-06-18 15:21:40:946 EDT] Logging output too large. Truncating
 output. [{estimated=true, rank=236.0, duration=185352.0,
 difficulty=5.0, total=88467.5, percentile=94.8197862291822,
 talents=[{id=196607.0, name=Eye of the Tiger,
 icon=ability_druid_primalprecision.jpg}, {id=116841.0,
 icon=ability_monk_tigerslust.jpg, name=Tiger's Lust}, {name=Fist of
 the White Tiger, id=261947.0, icon=inv_fistofthewhitetiger.jpg},
 {id=116844.0, icon=spell_monk_ringofpeace.jpg, name=Ring of Peace},
 {name=Inner Strength, icon=ability_monk_domeofmist.jpg, id=261767.0},
 {name=Hit Combo, icon=ability_monk_palmstrike.jpg, id=196740.0},
 {name=Whirling Dragon Punch, icon=ability_monk_hurricanestrike.jpg,
 id=152175.0}],
 corruption={activePowers=[{icon=inv_wand_1h_nzothraid_d_01.jpg,
 name=Ineffable Truth, id=318303.0, corruption=12.0}, {name=Lash of the
 Void, corruption=25.0, id=317290.0,
 icon=spell_priest_voidtendrils.jpg}, {corruption=25.0, name=Lash of
 the Void, id=317290.0, icon=spell_priest_voidtendrils.jpg}],
 passivePowers=[{name=Versatile, corruption=20.0, id=315553.0,
 icon=spell_arcane_arcanetactics.jpg}, {name=Versatile, id=315553.0,
 icon=spell_arcane_arcanetactics.jpg, corruption=20.0},
 {corruption=10.0, icon=spell_arcane_arcanetactics.jpg, name=Versatile,
 id=315549.0}, {id=315553.0, name=Versatile,
 icon=spell_arcane_arcanetactics.jpg, corruption=20.0},
 {name=Versatile, corruption=20.0, id=315553.0, icon=

标签: javascriptapigoogle-sheets

解决方案


它包含在一个数组中,这是您访问它的方式:data[0]['percentile'];

如果它只是一个普通的 JSON 对象,那么您的实现将是正确的。由于它不是并且已经被包装在一个数组中(JSON.parse() 执行此操作),因此您需要在访问值之前指定数组中的位置。希望这可以帮助!


推荐阅读