关于我在这里的问题
我想解析JSON数组中没有键的JSON数组,并将其放在Model类中。
这是我要解析的JSON数组。
[ { "pk": 100, "user": 5, "name": "Flutter", "details": "Fluttery", "images": [ 89, 88, 87, 86 ], "priority": 5 }, { "pk": 99, "user": 5, "name": "", "details": "h", "images": [], "priority": 5 }, { "pk": 98, "user": 5, "name": "Flutter", "details": "Fluttery", "images": [ 85 ], "priority": 5 }, ]
我已经成功解析了主数组,但是无法解析images包含整数数组的键。我想将其放入Model类。请帮忙。
images
谢谢!
您可以这样操作:
final jsonList = json.decode(response.body) as List; final userList = jsonList.map((map) => User.fromJson(map)).toList();
用户类别
class User { final int pk; final String name; final List<int> images; User._({this.pk, this.name, this.images}); factory User.fromJson(Map<String, dynamic> json) { return new User._( pk: json['pk'], name: json['name'], images: (json['images'] as List).map((map) => int.parse("$map")).toList()); } }
打印数据
for (var i = 0; i < userList.length; i++) { print(userList[i].name); final imageList = userList[i].images; for (var j = 0 ; j < imageList.length; j++){ print("image: ${imageList[j]}"); } }