【问题标题】:How do I access a data nested in JSON in javascript?如何在 javascript 中访问嵌套在 JSON 中的数据?
【发布时间】: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
 }

【问题讨论】:

标签: javascript arrays json google-apps-script google-sheets


【解决方案1】:

如果数据结构确实与您指定的一样,那么您尝试的其中一种尝试应该会奏效。请参阅下面的功能示例。

const data = {
  "0x123454843eacf5c5318e1234504251b937d12345": [
    {
      "poolIndex": 0,
      "stakingStrategy": "masterchef",
      "farmName": "sushiChef",
    }
  ]
}

const walletAddress = "0x123454843eacf5c5318e1234504251b937d12345";

console.log(data[walletAddress][0].poolIndex);

console.log(data[walletAddress][0].stakingStrategy);

【讨论】:

  • 谢谢!仍然无法正常工作。我添加了这个console.log(theparsedJSONdata[walletAddress][0].stakingStrategy);,我收到以下错误:TypeError: Cannot read property '0' of undefined parseTheDataFunctionSushi
  • pullAndParseAPISushi(walletAddress, networkName) 是否正在执行诸如获取数据之类的异步操作?
  • 我在谷歌表格中试图从 Zapper API 中提取数据以跟踪 Sushiswap 中的位置。
  • 如果你直接console.log(theparsedJSONdata),你会得到什么?
  • { '0x123454843eacf5c5318e1234504251b937d12345': [ { poolIndex: 0, stakingStrategy: 'masterchef', farmName: 'sushiChef', rewardAddress: '0x0000000', tokenAddress: '0x000000', rewardTokenS :18,rewardTokenAddress:'0x000000',rewardTokenPrice:12.12,isActive:true,标签:'WMATIC / ETH',协议:'sushiswap',...
【解决方案2】:

你试过了吗?

theparsedJSONdata.walletAddress[0].poolIndex

没有足够的代表发表评论,但我认为这可能有效,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多