我有一个json数据,我正在使用List<Map<String, dynamic>> jsonDecode.加载数据。
例如,我有一个;
我需要过滤子来获得成熟度值:689.19
另外,如果我有一个数值= 12500和值= 689.19,我需要得到口径,也就是24。
如何过滤List> json数据以获得一个颤振值?
[
{"amount": "5000", "caliber": "12", "maturity": "484.25"},
{"amount": "5000", "caliber": "24", "maturity": "275.67"},
{"amount": "7500", "caliber": "12", "maturity": "726.38"},
{"amount": "7500", "caliber": "24", "maturity": "413.51"},
{"amount": "10000", "caliber": "12", "maturity": "968.50"},
{"amount": "10000", "caliber": "24", "maturity": "551.35"},
{"amount": "12500", "caliber": "12", "maturity": "1210.63"},
{"amount": "12500", "caliber": "24", "maturity": "689.19"},
{"amount": "15000", "caliber": "12", "maturity": "1452.76"},
{"amount": "15000", "caliber": "24", "maturity": "827.03"},
{"amount": "15000", "caliber": "12", "maturity": "1694.89"},
{"amount": "17500", "caliber": "24", "maturity": "964.87"},
{"amount": "17500", "caliber": "36", "maturity": "727.66"},
{"amount": "17500", "caliber": "48", "maturity": "613.53"},
{"amount": "17500", "caliber": "60", "maturity": "548.44"},
{"amount": "20000", "caliber": "12", "maturity": "1937.01"},
{"amount": "20000", "caliber": "24", "maturity": "1102.71"}
]更新:我终于得到了@Muldec帮助下的。
首先,我加载上面的儿子列表如下所示。然后应用@Muldec的答案,它就成功了。
List<Map> myList = List<Map>.from(jsonDecode(jsonFastCalculation) as List);
final item = myList.firstWhere((e) => e['amount'] == '12500' && e['caliber'] == '24');
print(item['maturity']);发布于 2019-05-14 12:29:51
根据搜索条件,可以使用列表中的firstWhere或where方法获取所需的Map元素或Map列表。
假设您绝对确信搜索条件只给您一个项(或者您只关心满足条件的第一个项),下面是一个firstWhere代码示例
List<Map<String, dynamic>> myList = ....;
final item = myList.firstWhere((e) => e['amount'] == '12500' && e['caliber'] == '24');
print(item['maturity']);在firstWhere方法中,根据数量和成熟度找到口径只是改变测试的一件事。
https://stackoverflow.com/questions/56130163
复制相似问题