简体   繁体   English

如何访问嵌套在 javascript 中的 JSON 中的数据?

[英]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.我已经看到了这个 SO question ,但我仍然无法弄清楚。

I have some Jason data that I know begins like this:我有一些 Jason 数据,我知道这些数据是这样开始的:

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

I've written the following to get at the information like "poolIndex" and "stakingStrategy":我编写了以下内容来获取诸如“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)

This does not work.这不起作用。 I've written firstCrack every way I can think of我已经用我能想到的所有方式写firstCrack

  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(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

Here's the other function:这是另一个 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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