使用教程中的示例,您可以执行以下操作:
class Users { final List<Post> users; Users({this.users}); factory Users.fromJson(Map<String, dynamic> json) { List<Post> tempUsers = []; for (int i = 0; i < json['users'].length; i++) { Post post = Post.fromJson(json['users'][i]); tempUsers.add(post); } return Users(users: tempUsers); }}这是教程中的Post类:
class Post { final int userId; final int id; final String title; final String body; Post({this.userId, this.id, this.title, this.body}); factory Post.fromJson(Map<String, dynamic> json) { return Post( userId: json['userId'], id: json['id'], title: json['title'], body: json['body'], ); }}要显示标题和正文列表,您可以在本教程上更改FutureBuilder,如下所示:
final Future<Users> users;
…
FutureBuilder<Users>( future: users, builder: (context, snapshot) { if (snapshot.hasData) { return ListView.builder( itemCount: snapshot.data.users.length, itemBuilder: (context, index) { return Column( children: <Widget>[ Text('Title: ${snapshot.data.users[index].title}'), Text('Body: ${snapshot.data.users[index].body}'), ], ); }, ); } else if (snapshot.hasError) { return Text("${snapshot.error}"); } // By default, show a loading spinner. return CircularProgressIndicator(); },),我推荐您这篇文章,以了解有关解析JSON的更多信息: 在Flutter中解析复杂的JSON
此外,您还可以在此处找到有关如何进行手动序列化和自动序列化的更多信息:
JSON和序列化



