[英]How do I access a data nested in JSON in javascript?
几个小时以来,我一直试图弄清楚这一点。 我已经看到了这个 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:
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
}
你有没有尝试过?
theparsedJSONdata.walletAddress[0].poolIndex
没有足够的代表发表评论,但我认为这可能有效,请告诉我!
如果数据结构确实如您所指定,那么您尝试的尝试之一应该有效。 请参阅下面的功能示例。
const data = { "0x123454843eacf5c5318e1234504251b937d12345": [ { "poolIndex": 0, "stakingStrategy": "masterchef", "farmName": "sushiChef", } ] } const walletAddress = "0x123454843eacf5c5318e1234504251b937d12345"; console.log(data[walletAddress][0].poolIndex); console.log(data[walletAddress][0].stakingStrategy);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.