这是我的对象
class CustomerDomain {
String customerID;
String citizenID;
String status;
String title;
String name;
String email;
String phoneNumber;
Map<String, ServiceDetail> serviceDetails;
int remainingMinute;
Map<String, ReferenceChannel> referenceChannels;
String omiseCustomerID;
CustomerDomain({
this.customerID,
this.citizenID,
this.status,
this.title,
this.name,
this.email,
this.phoneNumber,
this.serviceDetails,
this.remainingMinute,
this.referenceChannels,
this.omiseCustomerID,
});
factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
return CustomerDomain(
customerID: parsedJson['customerID'],
citizenID: parsedJson['citizenID'],
status: parsedJson['status'],
title: parsedJson['title'],
name: parsedJson['name'],
email: parsedJson['email'],
phoneNumber: parsedJson['phoneNumber'],
serviceDetails: parsedJson['serviceDetailsails'],
remainingMinute: parsedJson['remainingMinute'],
referenceChannels: parsedJson['referenceChannels'],
omiseCustomerID: parsedJson['omiseCustomerID'],
);
}
}在调用服务之后,我像这样返回响应
if (response.statusCode == 200) {
print('entranceService3');
return CustomerDomain.fromJson(json.decode(response.body));
}我像customerID一样打印值,它就可以工作了。但是我不能使用从referenceChannels打印ReferenceChannel中的值。当我将referenceChannels转换成列表,然后是字符串,并打印出来。我得到了像这样的东西
[{channel:channel1,code:code1,secondCode:code2}]因此,我认为我没有正确地映射json,因为当我尝试将类型为Map的值转换为对象的值时,json不能正常工作。
发布于 2021-01-22 23:15:04
正如您已经知道的,您不能这样做:
customDomain: parsedJson但是,您需要一个CustomDomain.fromJson来映射值并确保类型安全。
因此,同样的概念也适用于ServiceDetailsails和ReferenceChannels。您需要解析整个对象,如下所示:
factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
return CustomerDomain(
customerID: parsedJson['customerID'],
citizenID: parsedJson['citizenID'],
status: parsedJson['status'],
title: parsedJson['title'],
name: parsedJson['name'],
email: parsedJson['email'],
phoneNumber: parsedJson['phoneNumber'],
serviceDetails: ServiceDetailsails.fromJson(parsedJson['serviceDetailsails']),
remainingMinute: parsedJson['remainingMinute'],
referenceChannels: ReferenceChannels.fromJson(parsedJson['referenceChannels']),
omiseCustomerID: parsedJson['omiseCustomerID'],
);}如果您使用的是dart,您还可以查看此文档以了解更多详细信息:https://flutter.dev/docs/development/data-and-backend/json
发布于 2021-01-22 22:56:00
Map也是一个类似于JSON的键值,因此它看起来像是
{
"referenceChannels": {
"key":"value"
}}如果您的值是另一个对象,则
"key":{"key1":"value","key1":"value" }总体而言,就像这样:
{
"referenceChannels": {
"key":{"key1":"value","key2":"value" }
}您将不得不像对CustomerDomain所做的那样遍历Reference Channel Object JSON。您还可以在Reference Channel类中创建FromJson()方法,并在Customer Domain的fromJson中使用该方法,如下所示:
factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
return CustomerDomain(
// Add other fields here
referenceChannels: parsedJson['referenceChannels'].fromJson,
);
}还可以尝试在https://app.quicktype.io/中创建类。此网站将使用toJson()和FromJson()方法创建类
发布于 2021-01-22 22:59:00
确保您的JSON字符串是正确的JSON字符串,在您的示例中,该字符串应如下所示:
'[{"channel":"channel1","code":"code1","secondCode":"code2"}]'看看这段代码,我使用了JSON.parse函数,它可以正常工作。
let jsonString = '[{"channel":"channel1","code":"code1","secondCode":"code2"}]';
let jsonArray = JSON.parse(jsonString);
console.log('channel name: 'jsonArray[0].channel);
console.log(jsonArray);
https://stackoverflow.com/questions/65847179
复制相似问题