这是我的对象
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
答案 0 :(得分:0)
Map 也是一个类似于 JSON 的键值,所以它看起来像一些东西
{
"referenceChannels": {
"key":"value"
}
} 如果您的值是另一个对象,则
"key":{"key1":"value","key1":"value" }
总体来说是这样的:
{
"referenceChannels": {
"key":{"key1":"value","key2":"value" }
}
您必须像在 CustomerDomain 中一样遍历引用通道对象 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() 方法创建您的类
答案 1 :(得分:0)
确保您的 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);
答案 2 :(得分:0)
正如你已经知道的那样,你不能做这样的事情:
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