一、準備數(shù)據(jù)源
// mockData.dart
abstract class JsonString{
static final String mockdata = ''' {
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
"score" : 111,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}''';
}
二、添加第三方庫
dependencies:
flutter:
sdk: flutter
json_serializable: ^3.2.0
json_annotation: ^3.0.0
build_runner: ^1.6.6
dev_dependencies:
flutter_test:
sdk: flutter
添加這json_serializable庫 和json_annotation,build_runner兩個依賴,這個三個庫可在https://pub.dev 中搜索最新的版本。
三、創(chuàng)建Model
// peesonModel.dart
class Data{
final String by;
final int descendants;
final int id;
final List<int> kids;
final int score;
final int time;
final String title;
final String type;
final String url;
Data({this.by, this.descendants, this.id, this.kids, this.score, this.time,
this.title, this.type, this.url});
}
四、自動化配置
// peesonModel.dart
import 'package:json_annotation/json_annotation.dart';
part 'peesonModel.g.dart'; // 我的文件名是peesonModel.dart
@JsonSerializable()
class Data {
final String by;
final int descendants;
final int id;
final List<int> kids;
final int score;
final int time;
final String title;
final String type;
@JsonKey(nullable: false)
final String url;
Data({this.by, this.descendants, this.id, this.kids, this.score, this.time,
this.title, this.type, this.url});
五、生成Json解析文件
使用JsonSerializable生成代碼的話必須要在需要生成代碼的實體類前添加注解@JsonSerializable(),而要使用這個注解我們必須引入json_annotation/json_annotation.dart這個包。
build_runner是dart團隊提供的一個生成dart代碼文件的外部包,用于自動化構建Json解析文件。
我們在當前項目的目錄下運行如下命令:
flutter packages pub run build_runner build

生成peesonModel.g.dart文件
這個peesonModel.g.dart是build_runner根據(jù)JsonSerializable生成的json解析文件。
我們來看看這個生成的dart文件
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'peesonModel.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Data _$DataFromJson(Map<String, dynamic> json) {
return Data(
by: json['by'] as String,
descendants: json['descendants'] as int,
id: json['id'] as int,
kids: (json['kids'] as List)?.map((e) => e as int)?.toList(),
score: json['score'] as int,
time: json['time'] as int,
title: json['title'] as String,
type: json['type'] as String,
url: json['url'] as String,
);
}
Map<String, dynamic> _$DataToJson(Data instance) => <String, dynamic>{
'by': instance.by,
'descendants': instance.descendants,
'id': instance.id,
'kids': instance.kids,
'score': instance.score,
'time': instance.time,
'title': instance.title,
'type': instance.type,
'url': instance.url,
};
_$DataFromJson:它接收了一個map:Map<String, dynamic>,并將這個Map里的值映射為我們所需要的實體類對象。我們就可以使用這個方法,將存有json數(shù)據(jù)的map轉化為我們需要的實體類對象。
_$DataToJson:將調(diào)用此方法的對象直接根據(jù)字段映射成Map。
六、重新構建peesonModel.dar文件中Data類
import 'package:json_annotation/json_annotation.dart';
part 'peesonModel.g.dart';
@JsonSerializable()
class Data {
final String by;
final int descendants;
final int id;
final List<int> kids;
final int score;
final int time;
final String title;
final String type;
final String url;
// 聲明一個和類名相同的函數(shù),來作為類的構造函數(shù)。
// this關鍵字指向了當前類的實例, 上面的代碼可以簡化為:
Data(
{this.by,
this.descendants,
this.id,
this.kids,
this.score,
this.time,
this.title,
this.type,
this.url});
// 使用命名構造函數(shù)從另一類或現(xiàn)有的數(shù)據(jù)中快速實現(xiàn)構造函數(shù)。
// 一個User.fromJson 構造函數(shù), 用于從一個map構造出一個 User實例 map structure
factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
// 一個toJson 方法, 將 User 實例轉化為一個map.
Map<String, dynamic> toJson() => _$DataToJson(this);
}
/**
* _$DataFromJson:它接收了一個map:Map<String, dynamic>,并將這個Map里的值映射為我們所需要的實體類對象。
* 我們就可以使用這個方法,將存有json數(shù)據(jù)的map轉化為我們需要的實體類對象。
*
* _$DataToJson:將調(diào)用此方法的對象直接根據(jù)字段映射成Map。
*
* 而這兩個都是私有方法,part讓兩個文件共享作用域與命名空間,所以我們需要將生成的方法暴露給外部。
* 然后我們再回到實體類中將 添加fromJson 和 toJson方法。
* */
七、業(yè)務場景使用
import 'peesonModel.dart';
import 'mockData.dart';
import 'dart:convert';
// json轉成Map ,依賴庫dart:convert, Map<String, dynamic> user = json.decode(json);
Map dataMap = json.decode(JsonString.mockdata);
// Map轉成Model
Data data1 = Data.fromJson(dataMap);
// Model轉成Map
Map dataMap1 = data1.toJson();
至此,我們已經(jīng)實現(xiàn)了 json轉成Map, Json 轉 Model ,Model轉成Map。