flutter 解析復雜json

之前的文章記錄了解析簡單json的方法、今天遇到復雜的格式、記錄作為參考

接口:http://news-at.zhihu.com/api/4/news/latest (知乎開放接口)

數(shù)據(jù)格式:



json分析: 包含一string字符串兩個list,所以要建立兩個class分別為top_stories、stories,其中top_stories的images為數(shù)組所以要使用 list<String>?images這種方式

bean類:

import 'package:json_annotation/json_annotation.dart';

part 'Sydata.g.dart';

@JsonSerializable()

class Sysdata? {

final String date;

final List<Staets> stories;

@JsonKey(name: "top_storie")

final List<Topstate> topstories;

Sysdata({this.date,this.stories,this.topstories});

? factory Sysdata.fromJson(Map<String,dynamic> parms) =>_$SysdataFromJson(parms);

? Map<String,dynamic> toJson() => _$SysdataToJson(this);

}

@JsonSerializable()

class Staets {

List <String> images;

int type;

int id;

@JsonKey(name: "ga_prefix")

String gaprefix;

String title;

Staets({this.images,this.type,this.id,this.gaprefix,this.title});

factory Staets.fromJson(Map<String,dynamic> parms) => _$StaetsFromJson(parms);

Map<String,dynamic> toJson() =>_$StaetsToJson(this);

}

@JsonSerializable()

class Topstate{

? String image;

? int type;

? int id;

? @JsonKey(name: "ga_prefix")

? String gaprefix;

? String title;

? Topstate({this.image,this.type,this.id,this.gaprefix,this.title});

? factory Topstate.fromJson(Map<String,dynamic> parms) =>_$TopstateFromJson(parms);

? Map <String,dynamic> toJson() =>_$TopstateToJson(this);

}


注意dart并不能直接轉(zhuǎn)譯 “xx-xx”字符串所以使用?@JsonKey(name:"") 進行包裹

解析方法:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容