[英]How can I parse this Json into a list in dart?
this response is what I get from a get request.But I dont know how can I convert it and put it on a list.this is a response for ohlc request from coingecko api.I want to know how can I use it in dart.这个响应是我从 get 请求中得到的。但我不知道如何转换它并将其放在列表中。这是对 coingecko api 的 ohlc 请求的响应。我想知道如何在 dart 中使用它。
[
[
1655830800000,
21401.69,
21401.69,
21401.69,
21401.69
],
[
1655832600000,
21404.99,
21404.99,
21372.94,
21394.43
],
]
I must say I am not completely sure what kind of data structure you want this to parsed into.我必须说我不完全确定您希望将其解析为哪种数据结构。 But the following solutions makes it a List<List<num>>
which is the closest to the way we can represent the input:但是以下解决方案使其成为List<List<num>>
最接近我们可以表示输入的方式:
import 'dart:convert';
void main() {
List<dynamic> jsonObject = jsonDecode(jsonString) as List<dynamic>;
List<List<num>> listOfListsOfDouble = [
for (final jsonListOfDoubles in jsonObject)
[
for (final value in (jsonListOfDoubles as List<dynamic>))
value as num
]
];
listOfListsOfDouble.forEach(print);
// [1655830800000, 21401.69, 21401.69, 21401.69, 21401.69]
// [1655832600000, 21404.99, 21404.99, 21372.94, 21394.43]
print(listOfListsOfDouble.runtimeType); // List<List<num>>
}
final jsonString = '''
[
[
1655830800000,
21401.69,
21401.69,
21401.69,
21401.69
],
[
1655832600000,
21404.99,
21404.99,
21372.94,
21394.43
]
]
'''
The reason I have used num
is because your list contains a mix of values that can be represented as double
or int
.我使用num
的原因是因为您的列表包含可以表示为double
或int
的混合值。 If we want to just convert any int
to double
and end up with List<List<double>>
we can just change the code to:如果我们只想将任何int
转换为double
并以List<List<double>>
结尾,我们可以将代码更改为:
List<List<double>> listOfListsOfDouble = [
for (final jsonListOfDoubles in jsonObject)
[
for (final value in (jsonListOfDoubles as List<dynamic>))
(value as num).toDouble()
]
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.