flutter : 嵌套的 json 解析列表
Posted
技术标签:
【中文标题】flutter : 嵌套的 json 解析列表【英文标题】:flutter : nested json parsing list 【发布时间】:2020-05-21 00:47:41 【问题描述】:我正在尝试从我的 json 代码中调用名称和费用 它是我的 json 主数组中的嵌套数组,主数组我可以处理它,但子数组我不能
"guideExtraServices": [
"Name": "Limousine",
"Fees": 100
,
"Name": "Bus",
"Fees": 10000
,
"Name": "Mini-Bus",
"Fees": 5000
],
我不能这样做,因为在我试图调用“姓名”和“费用”时出现错误
type 'List<ExtraServices>' is not a subtype of type 'String'
这是我的类,用于映射导游数据以在列表视图中使用它
class TourGuide
String id;
String name;
String email;
String password;
List<ExtraServices> extraService;
TourGuide(
this.id,
this.name,
this.email,
this.password,
this.extraService,
);
TourGuide.fromJson(Map<String, dynamic> json)
List<dynamic> extra = json['guideExtraServices'];
List<ExtraServices> extraList = extra.map((i) => ExtraServices.fromJson(i)).toList();
id = json['id'].toString();
name = json['displayName'];
email = json['email'];
password = json['password'];
extraService=extraList;
这是一个额外的服务类,导游类依赖它来获取子数组
class ExtraServices
String name;
double fees;
ExtraServices(
this.name,
this.fees
);
ExtraServices.fromJson(Map<String, dynamic> json)
name = json['Name'];
fees = json['Fees'].toDouble();
我使用 api 解码 json 的提供者方法
Future<dynamic> tourGuideList() async
_isLoading = true;
notifyListeners();
print('Starting request');
http.Response response = await http.get(Environment.tourGuide,
headers: Environment.requestHeader);
print('Completed request');
print('respond data : $response.body');
Map<String, dynamic> res = json.decode(response.body);
var results;
if (res['code'] == 200)
print('start load tourguide');
_tourGuide = [];
res['message'].forEach((v)
_tourGuide.add(new TourGuide.fromJson(v));
);
results = true;
else
results =
FailedRequest(code: 400, message: res['error'], status: false);
_isLoading = false;
notifyListeners();
return results;
我不知道为什么我有一个错误,我无法修复它
【问题讨论】:
查看这个高级 JSON 解析示例。 ***.com/a/60224846/3756408 【参考方案1】:请尝试以下代码:-
首先创建模型:-
class GuideResponseModel
List<GuideExtraServicesModel> guideExtraServiceList;
GuideResponseModel(
this.guideExtraServiceList
);
factory GuideResponseModel.fromJson(Map<String, dynamic> parsedJson)
try
List<GuideExtraServicesModel> guideExtraServiceModelList = new List();
if (parsedJson.containsKey('guideExtraServices'))
var countryList = parsedJson['guideExtraServices'] as List;
guideExtraServiceModelList =
countryList.map((i) => GuideExtraServicesModel.fromJson(i)).toList();
return GuideResponseModel(
guideExtraServiceList: guideExtraServiceModelList
);
catch (e)
return null;
class GuideExtraServicesModel
String name;
int fees;
GuideExtraServicesModel(this.name,this.fees);
factory GuideExtraServicesModel.fromJson(Map<String, dynamic> json)
return GuideExtraServicesModel(name: json['Name'],fees: json['Fees']);
模型的第二个用户:-
String jsonData = '"guideExtraServices": ["Name": "Limousine","Fees": 100,"Name": "Bus","Fees": 10000,"Name": "Mini-Bus","Fees": 5000]';
final dynamic jsonResponse = json.decode(jsonData);
final GuideResponseModel responseModel = GuideResponseModel.fromJson(jsonResponse);
print('======$responseModel.guideExtraServiceList[0].name----$responseModel.guideExtraServiceList[0].fees');
【讨论】:
【参考方案2】:我觉得你的json总共应该是这样的:
"guideExtraServices": [
"Name": "Limousine",
"Fees": 100
,
"Name": "Bus",
"Fees": 10000
,
"Name": "Mini-Bus",
"Fees": 5000
]
试试
// To parse this JSON data, do
//
// final tourGuide = tourGuideFromJson(jsonString);
import 'dart:convert';
TourGuide tourGuideFromJson(String str) => TourGuide.fromJson(json.decode(str));
String tourGuideToJson(TourGuide data) => json.encode(data.toJson());
class TourGuide
List<GuideExtraService> guideExtraServices;
TourGuide(
this.guideExtraServices,
);
factory TourGuide.fromJson(Map<String, dynamic> json) => TourGuide(
guideExtraServices: List<GuideExtraService>.from(json["guideExtraServices"].map((x) => GuideExtraService.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"guideExtraServices": List<dynamic>.from(guideExtraServices.map((x) => x.toJson())),
;
class GuideExtraService
String name;
int fees;
GuideExtraService(
this.name,
this.fees,
);
factory GuideExtraService.fromJson(Map<String, dynamic> json) => GuideExtraService(
name: json["Name"],
fees: json["Fees"],
);
Map<String, dynamic> toJson() =>
"Name": name,
"Fees": fees,
;
【讨论】:
先生,你为什么写这部分代码 Map以上是关于flutter : 嵌套的 json 解析列表的主要内容,如果未能解决你的问题,请参考以下文章
如何使用从 Flutter 中的 json 解析的嵌套映射列表中的函数创建对象
如果 Flutter/Dart 中没有名称/键,如何序列化/解析嵌套的 JSON 数组