簡體   English   中英

如何訪問嵌套在 javascript 中的 JSON 中的數據?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM