如何在颤动中从json中只选择一项
Posted
技术标签:
【中文标题】如何在颤动中从json中只选择一项【英文标题】:how to select just one item from json in flutter 【发布时间】:2021-06-26 02:29:01 【问题描述】:您好,我想在我的应用程序中显示 SPX 价格,并且有这个 json API 我需要从这个 API 中选择 SPX 数据我该怎么做??
["ask":"28.47500","bid":"27.47500","name":"USDUAH","time":"1616772920.000",
"ask":"3972.06000","bid":"3971.16000","name":"SPX","time":"1617047940.000",
"ask":"0.71965","bid":"0.71916","name":"AUDCHF","time":"1617076421.403"]]
【问题讨论】:
【参考方案1】:这可能有效
List data = parseResponse(response.body)
var spx ;
data.forEach((element)
if (element["name"]=="SPX")
spx =element; // access the spx object to get other data
);
static List<Trade> parseResponse(String responseBody) // for converting array of objects to object list
final parsed = json.decode(responseBody).cast<Map<dynamic, dynamic>>();
return parsed.map<Trade>((json) => Trade.fromJson(json)).toList();
```
【讨论】:
【参考方案2】:你可以这样做:
final lst = ["ask":"28.47500","bid":"27.47500","name":"USDUAH","time":"1616772920.000",
"ask":"3972.06000","bid":"3971.16000","name":"SPX","time":"1617047940.000",
"ask":"0.71965","bid":"0.71916","name":"AUDCHF","time":"1617076421.403"];
final res = lst.where((element) => element['name'] == 'SPX').toList();
查看 DartPad here 上的完整示例
【讨论】:
以上是关于如何在颤动中从json中只选择一项的主要内容,如果未能解决你的问题,请参考以下文章