Flutter中Json解析

json.decode

json.decode返回值为Map<String, dynamic>直到运行的时候才能知道具体的类型

所以不能:类型检查,自动提示,编译时无法发现错误

json_serializable

(1)pubspec.yaml中添加依赖

1
2
3
4
5
6
dependencies:
json_annotation: ^2.0.0 # https://pub.dartlang.org/packages/json_annotation

dev_dependencies:
build_runner: ^1.1.2 # https://pub.dartlang.org/packages/build_runner
json_serializable: ^2.0.1 # https://pub.dartlang.org/packages/json_serializable

(2)创建Model并添加注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import 'package:json_annotation/json_annotation.dart';

part 'person.g.dart';

@JsonSerializable()
class Person {
String name;
int age;

Person(this.name, this.age);

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}

(3)自动生成fromJson和toJson(model.g.dart)

1
flutter packages pub run build_runner build

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'package:flutter/material.dart';
import 'dart:convert';

import 'package:flutter_app/model/person.dart';

class JsonDemoPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _JsonDemoPagePageState();
}

class _JsonDemoPagePageState extends State<JsonDemoPage> {
String name = "test";
User user = null;
Person person = null;

@override
void initState() {
super.initState();
}

@override
void didUpdateWidget(covariant JsonDemoPage oldWidget) {
name = JsonDecoder.parseJson2Map();
user = JsonDecoder.parseJson2Object();
person = JsonDecoder.parseJsonSerialize();
}

Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('JSON Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('name: $name'),
SizedBox(height: 10,),
Text('User: [name: ${user.name}, homepage: ${user.homepage}]', textAlign: TextAlign.center,),
SizedBox(height: 10,),
Text('Person: ${person.toJson()}', textAlign: TextAlign.center,),
],
)
),
);
}
}

class JsonDecoder {

//json.decode返回值为Map<String, dynamic>直到运行的时候才能知道具体的类型
//所以不能:类型检查,自动提示,编译时无法发现错误,严重不推荐!
static String parseJson2Map() {
String jsonStr = '{"name": "Joe.Ye"}';
var user = json.decode(jsonStr);
print('${user['name']}');
return '${user['name']}';
}

//手写fromJson, toJson不太友好
static User parseJson2Object() {
String jsonStr = '{"name":"Joe.Ye", "email": "yezhou@yezhou.org", "homepage":"http://www.appblog.cn"}';
Map userMap = json.decode(jsonStr);
var user = new User.fromJson(userMap);
print('${user.name}');
print(user.toJson());
return user;
}

static Person parseJsonSerialize() {
String jsonStr = '{"name":"Joe.Ye", "age":30}';
Map personMap = json.decode(jsonStr);
return Person.fromJson(personMap);
}

//https://javiercbk.github.io/json_to_dart/
}

class User {
final String name;
final String email;
final String homepage;

User(this.name, this.email, this.homepage);

User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'],
homepage = json['homepage'];

Map<String, dynamic> toJson() => {
'name': name,
'email': email,
'homepage': homepage
};
}

Flutter Json

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :