dart JSON字符串转换为列表字符串 [英] dart JSON String convert to List String
问题描述
我有一个API,该API会按如下方式调用json字符串数组:
I have an API that calls the json String array as follows:
[
"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"
]
<我打算将json转换为List i飞镖。我尝试了以下脚本:
I intend to convert the json to the List in the dart. I tried the script below :
json.decode(response.body).cast<List<String>();
但没有用,脚本应该如何正确?
but it didn't work, how should the script be correct?
推荐答案
解析JSON列表的结果是 List< dynamic>
。 jsonDecode
的返回类型只是动态
。
The result of parsing a JSON list is a List<dynamic>
. The return type of jsonDecode
is just dynamic
.
您可以将这样的列表转换为 List< String>
作为
You can cast such a list to a List<String>
as
List<String> stringList = (jsonDecode(input) as List<dynamic>).cast<String>();
您也可以将其用作 List< dynamic>
,然后将每个值分配给 String
:
You can also just use it as a List<dynamic>
and then assign each value to 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.
这篇关于dart JSON字符串转换为列表字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!