如何访问嵌套数组的最后一个索引

How to Access Last Index of Nested Array

我想知道如何访问嵌套数组的最后一个索引。我正在使用的数组由 API 调用提供并且非常大,因此为了方便起见,我将在下面缩短它。

数组:

{

"result": {
    "86400": [
      [
        1381190400,
        123.610000,
        123.610000,
        123.610000,
        123.610000,
        0.100000,
        0.0
      ],
      [
        1381276800,
        123.610000,
        124.190000,
        123.900000,
        124.180000,
        3.991600,
        0.0
      ],
     ...

      [
        1600646400,
        11078,
        11078,
        10906.9,
        10950.4,
        623.00835437,
        6841501.73480653
      ]
    ]
  },

"allowance": {
    "cost": 0.015,
    "remaining": 9.985,
    "upgrade": "For unlimited API access, create an account at ________"
  }
}

我想访问 ['result']['86400'] 的最后一个索引,其中包含:

 [
        1600646400,
        11078,
        11078,
        10906.9,
        10950.4,
        623.00835437,
        6841501.73480653
      ]

我在我的代码中使用了 Flutter,所以这是我尝试过的:

http.Response historicalResponse = await http.get(historicalRequestURL);

var decodedHistorical = jsonDecode(historicalResponse.body);

var historicalPrice = decodedHistorical['result']['86400'.length - 1][0];

print(historicalPrice);

这会导致一些错误:

"NoSuchMethodError: The method '[]' was called on null."
"Receiver: null"
"Tried calling: [](0)"

我认为 ['86400'.length - 1] 导致了错误。

我也试过使用

var historicalPrice = decodedHistorical['result']['86400'][decodedHistorical.length - 1][0];

相反。这不会导致错误,但它会给出外部数组的长度,即 2,但我需要名为“86400”的内部数组的长度。

不要使用 var,而是使用 List 以便您可以通过 historicalPrice.last getter.

访问它的最后一个元素
List historicalPrice = decodedHistorical['result']['86400'];
List lastElement=historicalPrice.last;

您应该按照以下方式实施

 void parseJson() async {
    http.Response historicalResponse = await http.get(historicalRequestURL);
    Map decodedHistorical = jsonDecode(historicalResponse.body);
    List selectedMap = decodedHistorical['result']['86400'];
    var finalValue = selectedMap.last;
    print(finalValue);
  }