在flutter中解析嵌套的JSON列表时出错
Posted
技术标签:
【中文标题】在flutter中解析嵌套的JSON列表时出错【英文标题】:Error in parsing nested JSON list in flutter 【发布时间】:2019-06-17 02:50:45 【问题描述】:调用 API 但显示错误,我无法解析 bean 类中的数据。
我的回应:
"error":"0",
"status":200,
"deliveryCharge":"14.00",
"image_base_url":"http:\/\/xxxxx.tk\/assets\/event\/image\/",
"image_logo_url":"http:\/\/xxxxx.tk\/assets\/event\/logo\/",
"eventList":[
"event_id":"1",
"event_name":"Syscraft Premier League",
"event_location":"12 ny valleys",
"event_type_id":"15",
"start_date":"2019-01-10 03:21:00",
"end_date":"2019-01-26 16:10:00",
"event_logo":"f4f0bfc168a3816891e2749232c5243f.jpg"
,
"event_id":"3",
"event_name":"Republic Day Event 2019",
"event_location":"AH-654 Villa No. 42 New Township New Township",
"event_type_id":"1",
"start_date":"2019-01-26 00:00:00",
"end_date":"2019-01-26 11:55:00",
"event_logo":"3a4a7fabbbd7ed8febf67bacda71ae48.jpg"
]
调用接口
Future<List<EventResponse>> fetchEvent( ) async
String url='http://xxxxxxxxxxxxxxx.tk/api/userapp/event/lists';
var headers = new Map();
headers['Auth-Key'] = 'OCDOC@2018';
headers['End-Client'] = 'OCDOC';
var body = new Map();
headers['schedule'] = 'present';
http.Response res = await http.post(url,headers: headers, body: body);
final Map<String,dynamic> parsed=json.decode(res.body); // post api call
print("Reposnce Event:---"+parsed.toString());
我的豆类
class EventResponse
String error;
int status;
String deliveryCharges;
String imageBaseUrl;
String imageLogoUrl;
List<Event> eventList;
EventResponse(
this.error,
this.status,
this.deliveryCharges,
this.imageBaseUrl,
this.imageLogoUrl,
this.eventList
);
factory EventResponse.convertEventResponse(Map<String,dynamic> json)
return EventResponse(
error: json['error'],
status: json['status'],
deliveryCharges: json['deliveryCharge'],
imageBaseUrl: json['image_base_url'],
imageLogoUrl: json['image_logo_url'],
eventList: json['eventList']);
class Event
String eventId;
String eventName;
String location;
String event_logo;
Event(
this.eventId,
this.eventName,
this.location,
this.event_logo,
);
factory Event.convertEvent(Map<String,dynamic> json)
return Event(
eventId: json['event_id'],
eventName: json['event_name'],
location: json['event_location'],
event_logo: json['event_logo'],
);
显示错误
_InternalLinkedHashMap<dynamic, dynamic> is not a subtype of type Map<String, String>
【问题讨论】:
【参考方案1】:像这样重写EventResponse
:
class EventResponse
String error;
int status;
String deliveryCharges;
String imageBaseUrl;
String imageLogoUrl;
List<Event> eventList;
EventResponse(
this.error,
this.status,
this.deliveryCharges,
this.imageBaseUrl,
this.imageLogoUrl,
this.eventList);
factory EventResponse.convertEventResponse(Map<String, dynamic> json)
List<dynamic> events = json['eventList'];
List<Event> eventList = events.map((e) => Event.convertEvent(e)).toList();
return EventResponse(
error: json['error'],
status: json['status'],
deliveryCharges: json['deliveryCharge'],
imageBaseUrl: json['image_base_url'],
imageLogoUrl: json['image_logo_url'],
eventList: eventList,
);
【讨论】:
【参考方案2】:我已经像@Richard Heap 一样更改了EventResponse
。
factory EventResponse.convertEventResponse(Map<String, dynamic> json)
List<dynamic> events = json['eventList'];
List<Event> eventList = events.map((e) => Event.convertEvent(e)).toList();
return EventResponse(
error: json['error'],
status: json['status'],
deliveryCharges: json['deliveryCharge'],
imageBaseUrl: json['image_base_url'],
imageLogoUrl: json['image_logo_url'],
eventList: eventList,
);
我需要更改的另一件事是当我发布parameters
和headers
时需要将他们的Map()
定义为Map<String,String>()
。
Future<EventResponse> fetchEvent( ) async // here i change Future type
String url='http://xxxxxxx-oceanapparel.tk/api/userapp/event/lists';
var headers = new Map<String, String>(); //here i defined Map type
headers['Auth-Key'] = 'OCDOC@2018';
headers['End-Client'] = 'OCDOC';
var body = new Map<String, String>(); //here i defined Map type
headers['schedule'] = 'present';
http.Response res = await http.post(url,headers: headers, body: body);
print("Reposnce Event:---"+res.body);
【讨论】:
以上是关于在flutter中解析嵌套的JSON列表时出错的主要内容,如果未能解决你的问题,请参考以下文章
如何使用从 Flutter 中的 json 解析的嵌套映射列表中的函数创建对象