简体   繁体   中英

How do I access a data nested in JSON in javascript?

I have been trying to figure this out for hours. I've seen this SO question and I still cannot figure this out.

I have some Jason data that I know begins like this:

{
  "0x123454843eacf5c5318e1234504251b937d12345": [
{
  "poolIndex": 0,
  "stakingStrategy": "masterchef",
  "farmName": "sushiChef",
 ....

I've written the following to get at the information like "poolIndex" and "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)

This does not work. I've written firstCrack every way I can think of

  theparsedJSONdata[walletAddress].poolIndex
  theparsedJSONdata[walletAddress][0].poolIndex

None of them work. So frustrating. Any help would be appreciated.

For what it's worth, `Object.keys(walletAddress) returns

 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

Here's the other 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
 }

Have you tried?

theparsedJSONdata.walletAddress[0].poolIndex

don't have enough rep to make a comment but I think this might work, let me know!

If the data structure is indeed as you specified, then one of the attempts you tried should work. See below for a functional example.

 const data = { "0x123454843eacf5c5318e1234504251b937d12345": [ { "poolIndex": 0, "stakingStrategy": "masterchef", "farmName": "sushiChef", } ] } const walletAddress = "0x123454843eacf5c5318e1234504251b937d12345"; console.log(data[walletAddress][0].poolIndex); console.log(data[walletAddress][0].stakingStrategy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM