var map = { "name": "zhangsan", "email": "123@qq.com", "age": 1 }; //转成字符串 String name = json.encode(map); print(name); //解析字符串 Map<String, dynamic> user = json.decode(name); print("姓名:${user['name']}"); print("邮箱:${user['email']}"); print("年龄:${user['age']}"); }
或者第二種
第二种:内置json创建model方式解析。 先建立model, class User { String name; String email; int age; User(this.name, this.email, this.age) {} User.fromJson(Map<String, dynamic>json) :name=json["name"], email = json["email"], age = json["age"]; Map<String, dynamic> toJson() { return { "name": name, "email": email, "age": age }; } } 然后可以使用了, var map = { "name": "zhangsan", "email": "123@qq.com", "age": 1 }; var user = User.fromJson(map); print("name:${user.name}"); var userMap = User("lisi", "321@qq.com", 3).toJson(); for (var key in userMap.keys) { print("${key} : ${userMap[key]}"); } 这种是比较常用的用来解析复杂的json
從https://www.cnblogs.com/wupeng88/p/10770135.html轉載
感謝大佬