Json解析和復(fù)雜模型轉(zhuǎn)換實用技巧
應(yīng)該使用哪種JSON序列化方式
- 小型項目:手動序列化;
- 大型項目借助插件生成json_serializable和built_value;
如何序列化
http://www.devio.org/io/flutter_app/json/test_common_model.json
{
"icon": "http://www.devio.org/io/flutter_app/img/ln_food.png",
"title": "美食林",
"url": "https://m.ctrip.com/webapp/you/foods/address.html?new=1&ishideheader=true",
"statusBarColor": "19A0F0",
"hideAppBar": true
}
解析:
Map<String,dynamic> map = JSON.decode(jsonStr);
print('icon':${map['icon']})
print('title':${map['title']})
將Map轉(zhuǎn)換成model
class CommonModel {
final String icon;
final String title;
final String url;
final String statusBarColor;
final bool hideAppBar;
CommonModel({this.icon, this.title, this.url, this.statusBarColor,
this.hideAppBar});
factory CommonModel.fromJson(Map<String, dynamic> json){
return CommonModel(
icon: json['icon'],
title: json['title'],
url: json['url'],
statusBarColor: json['statusBarColor'],
hideAppBar: json['hideAppBar']);
}
}
復(fù)雜JSON解析
如何解析對象中的數(shù)組
{
"url":"xxx",
"tabs":[
{
"labelName":"推薦",
"groupChannelCode":"tourphoto_global1"
},
{
"labelName":"拍照技巧",
"groupChannelCode":"tab—photo"
}
]
}
解析:
class TravelTabModel {
String url;
List<TravelTab> tabs;
TravelTabModel({this.url, this.tabs});
TravelTabModel.fromJson(Map<String, dynamic> json){
url = json['url'];
//先將tabs轉(zhuǎn)化成List,再將List中的值轉(zhuǎn)換成TravelTab
(json['tabs'] as List).map((i) => TravelTab.fromJson(i));
}
}
/**
* final寫法
*/
class TravelTabModel2 {
final String url;
final List<TravelTab> tabs;
TravelTabModel2({this.url, this.tabs});
factory TravelTabModel2.fromJson(Map<String, dynamic> json){
String url = json['url'];
List<TravelTab> tabs = (json['tabs'] as List).map((i) =>
TravelTab.fromJson(i)).toList();
return TravelTabModel2(url: url, tabs: tabs);
}
}
class TravelTab {
String labelName;
String groupChannelCode;
TravelTab({this.labelName, this.groupChannelCode});
TravelTab.fromJson(Map<String, dynamic> json){
labelName = json['labelName'];
groupChannelCode = json['groupChannelCode'];
}
}
轉(zhuǎn)換復(fù)雜大量數(shù)據(jù)
http://www.devio.org/io/flutter_app/json/home_page.json
使用在線轉(zhuǎn)換工具json_to_dart生成實體類
class Autogenerated {
Config config;
List<BannerList> bannerList;
List<LocalNavList> localNavList;
GridNav gridNav;
List<SubNavList> subNavList;
SalesBox salesBox;
Autogenerated(
{this.config,
this.bannerList,
this.localNavList,
this.gridNav,
this.subNavList,
this.salesBox});
Autogenerated.fromJson(Map<String, dynamic> json) {
config =
json['config'] != null ? new Config.fromJson(json['config']) : null;
if (json['bannerList'] != null) {
bannerList = new List<BannerList>();
json['bannerList'].forEach((v) {
bannerList.add(new BannerList.fromJson(v));
});
}
if (json['localNavList'] != null) {
localNavList = new List<LocalNavList>();
json['localNavList'].forEach((v) {
localNavList.add(new LocalNavList.fromJson(v));
});
}
gridNav =
json['gridNav'] != null ? new GridNav.fromJson(json['gridNav']) : null;
if (json['subNavList'] != null) {
subNavList = new List<SubNavList>();
json['subNavList'].forEach((v) {
subNavList.add(new SubNavList.fromJson(v));
});
}
salesBox = json['salesBox'] != null
? new SalesBox.fromJson(json['salesBox'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.config != null) {
data['config'] = this.config.toJson();
}
if (this.bannerList != null) {
data['bannerList'] = this.bannerList.map((v) => v.toJson()).toList();
}
if (this.localNavList != null) {
data['localNavList'] = this.localNavList.map((v) => v.toJson()).toList();
}
if (this.gridNav != null) {
data['gridNav'] = this.gridNav.toJson();
}
if (this.subNavList != null) {
data['subNavList'] = this.subNavList.map((v) => v.toJson()).toList();
}
if (this.salesBox != null) {
data['salesBox'] = this.salesBox.toJson();
}
return data;
}
}
class Config {
String searchUrl;
Config({this.searchUrl});
Config.fromJson(Map<String, dynamic> json) {
searchUrl = json['searchUrl'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['searchUrl'] = this.searchUrl;
return data;
}
}
class BannerList {
String icon;
String url;
BannerList({this.icon, this.url});
BannerList.fromJson(Map<String, dynamic> json) {
icon = json['icon'];
url = json['url'];
}
.....