Posts Flutter Json解析
Post
Cancel

Flutter Json解析

1
2
3
4
5
6
7
import 'dart:convert';
void main() {
  var jsonString = '{"name": "John Smith","email": "[email protected]"}';
  Map<String, dynamic> user = jsonDecode(jsonString);
  print('Howdy, ${user['name']}!');
  print('We sent the verification link to ${user['email']}.');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import 'dart:convert';
class User {
  final String name;
  final String email;
  User(this.name, this.email);
  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];
  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}
void main() {
    var jsonString = '{"name": "John Smith","email": "[email protected]"}';
    Map userMap = jsonDecode(jsonString);
	  var user = new User.fromJson(userMap);
	  print('Howdy, ${user.name}!');
	  print('We sent the verification link to ${user.email}.');
}

参考

This post is licensed under CC BY 4.0 by the author.

Dart语法总结

Handler的使用和原理分析

Comments powered by Disqus.