[英]dart JSON String convert to List String
I have an API that calls the json String array as follows:我有一个调用 json 字符串数组的 API,如下所示:
[
"006.01.01",
"006.01.01 1090",
"006.01.01 1090 1090.950",
"006.01.01 1090 1090.950 052",
"006.01.01 1090 1090.950 052 A",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 B",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.994",
"006.01.01 1090 1090.994 001",
"006.01.01 1090 1090.994 001 A",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111"
]
I intend to convert the json to the List in the dart.我打算将 json 转换为 dart 中的 List。 I tried the script below :我尝试了下面的脚本:
json.decode(response.body).cast<List<String>();
but it didn't work, how should the script be correct?但它不起作用,脚本应该如何正确?
The result of parsing a JSON list is a List<dynamic>
.解析 JSON 列表的结果是List<dynamic>
。 The return type of jsonDecode
is just dynamic
. jsonDecode
的返回类型只是dynamic
。
You can cast such a list to a List<String>
as您可以将这样的列表转换为List<String>
作为
List<String> stringList = (jsonDecode(input) as List<dynamic>).cast<String>();
You can also just use it as a List<dynamic>
and then assign each value to String
:您也可以将其用作List<dynamic>
,然后将每个值分配给String
:
List<dynamic> rellyAStringList = jsonDecode(input);
for (String string in reallyAStringList) { ... }
The effect is approximately the same - each element is checked for being a string when it is taken out of the list.效果大致相同——当每个元素从列表中取出时,都会检查它是否是一个字符串。
We can easily parse the JSON into a Dart array without the need of creating any class.我们可以轻松地将 JSON 解析为 Dart 数组,而无需创建任何类。
main() {
String arrayText = '{"tags": ["dart", "flutter", "json"]}';
var tagsJson = jsonDecode(arrayText)['tags'];
List<String> tags = tagsJson != null ? List.from(tagsJson) : null;
print(tags);
}
Now you can see the result of printing the Dart Array.现在您可以看到打印 Dart Array 的结果。
[dart, flutter, json]
Try this one.试试这个。 Hope it helps.希望能帮助到你。
import 'dart:convert';
void main() {
String jsonResponse = '''
["006.01.01",
"006.01.01 1090",
"006.01.01 1090 1090.950",
"006.01.01 1090 1090.950 052",
"006.01.01 1090 1090.950 052 A",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 B",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.994",
"006.01.01 1090 1090.994 001",
"006.01.01 1090 1090.994 001 A",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111"]
''';
dynamic jsonParsed = json.decode(jsonResponse);
// print(jsonParsed);
print(jsonParsed[5]);
}
Convert Json Data To List将 Json 数据转换为列表
List<String> data = [
"006.01.01",
"006.01.01 1090",
"006.01.01 1090 1090.950",
"006.01.01 1090 1090.950 052",
"006.01.01 1090 1090.950 052 A",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 A 521219",
"006.01.01 1090 1090.950 052 B",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.950 052 B 521211",
"006.01.01 1090 1090.994",
"006.01.01 1090 1090.994 001",
"006.01.01 1090 1090.994 001 A",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111",
"006.01.01 1090 1090.994 001 A 511111"
];
//your json string
String jsonString = json.encode(data);
//convert json string to list
List<String> newData = List<String>.from(json.decode(jsonString));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.