【发布时间】:2021-06-02 02:21:50
【问题描述】:
我已经尝试了几个小时来解决这个问题。我见过this SO question,但我仍然无法弄清楚。
我知道一些 Jason 数据是这样开始的:
{
"0x123454843eacf5c5318e1234504251b937d12345": [
{
"poolIndex": 0,
"stakingStrategy": "masterchef",
"farmName": "sushiChef",
....
我编写了以下内容来获取“poolIndex”和“stakingStrategy”等信息:
function parseTheDataFunctionSushi(walletAddress, networkName){
// calls a funciton to pull and parse the api data
var walletAddress = "0x123454843eacf5c5318e1234504251b937d12345";
var networkName = "polygon";
var theparsedJSONdata = pullAndParseAPISushi(walletAddress, networkName)
console.log("object keys are " + Object.keys(walletAddress));
var firstCrack = theparsedJSONdata[walletAddress][0]['poolIndex']
console.log("firstCrack is " + firstCrack)
这不起作用。我已经写了firstCrack我能想到的各种方式
theparsedJSONdata[walletAddress].poolIndex
theparsedJSONdata[walletAddress][0].poolIndex
它们都不起作用。太令人沮丧了。任何帮助将不胜感激。
对于它的价值,`Object.keys(walletAddress) 返回
object keys are 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
这是另一个函数:
function pullAndParseAPISushi (walletAddress, networkName){
console.log("walletAddress inside pullAndParseAPISushi is = " + walletAddress);
console.log("network is " + networkName);
var apiKEY = "96e0cc51-a62e-42ca-acee-910ea7d2a241"; // API key from Zapper
var url = "https://api.zapper.fi/v1/staked-balance/masterchef?addresses%5B%5D="+ walletAddress + "&network=" + networkName + "&api_key=" + apiKEY;
// assembles the API URL with the wallet addressa, network and name
console.log("url is " + URL);
var response = UrlFetchApp.fetch(url); // pulls data from the API
console.log(response)
var theparsedJSONdata = JSON.parse(response); // parses the JSON response from the API
console.log(theparsedJSONdata)
return theparsedJSONdata
}
【问题讨论】:
-
这是不可重现的。如果返回的 JSON 和代码与您提到的完全相同,
theparsedJSONdata[walletAddress][0].poolIndex应该返回 0。您可以在调用theparsedJSONdata[walletAddress][0]['poolIndex']之前尝试记录theparsedJSONdata吗?你得到了什么?
标签: javascript arrays json google-apps-script google-sheets