【发布时间】:2021-04-24 04:26:30
【问题描述】:
我有这段代码,我可以在其中将 json 数据放入一个列表中,但我真的不知道如何获取我想要的特定数据,比如值
main.dart
Future<String> loadDataFromJson() async {
return await rootBundle.loadString("assets/categories.json");
}
Future loadData() async {
String jString = await loadDataFromJson();
final jRes = json.decode(jString) as List;
List<Category> datas = jRes.map((e) => Category.fromJson(e)).toList();
print(datas);
}
@override
void initState() {
super.initState();
loadData();
}
我在这里打印了数据,它给了我I/flutter ( 6111): [Instance of 'Category', Instance of 'Category', Instance of 'Category', Instance of 'Category']
模型
class Category {
final String catId;
final String catName;
Category({this.catId, this.catName});
factory Category.fromJson(Map<String, dynamic> json) {
return Category(catId: json['cat_id'], catName: json['category']);
}
}
我的json是这样的,但是有多个
{
"category": "Design & Creativity",
"cat_id": "1",
"cat_suncategory": [
{
"sub_name": "Ads",
"sub_image": "https://images.unsplash.com/photo-1589838017489-9198a27bd040?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8YWR2ZXJ0aXNlbWVudHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
}
]
}
请问我如何获得我想要的价值
【问题讨论】: