我正在尝试弄清楚如何访问嵌套数组的最后一个索引。我正在使用的数组是由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‘的最后一个索引,它包含:
[
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‘的内部数组的长度。
发布于 2020-09-20 15:59:53
不使用var,而使用List,这样您就可以通过historicalPrice.last getter访问它的最后一个元素。
List historicalPrice = decodedHistorical['result']['86400'];
List lastElement=historicalPrice.last;https://stackoverflow.com/questions/63976399
复制相似问题