FHIR资源的飞镖/颤振中的Json之一
我正在尝试为Json中定义的FHIR资源创建Dart类。如果有人想看的话,请看完整的。我的问题是一份声明。具体地说,我有一个如下所示的类(我在这里不包括完整的类定义,但如果有人认为这会有帮助,我可以这样做): 我尝试将'resource'变量声明为'var'、'dynamic'和'ResourceList'类型,并创建了一个仅包含资源的ResourceList类 每个资源都有一个名为“resourceType”的字段,所以我还尝试创建了一个ResourceList函数,该函数根据“resourceType”参数返回不同的类型,但它也不起作用 如果我执行http请求,我尝试解析的实际响应如下所示:FHIR资源的飞镖/颤振中的Json之一,json,flutter,dart,hl7-fhir,Json,Flutter,Dart,Hl7 Fhir,我正在尝试为Json中定义的FHIR资源创建Dart类。如果有人想看的话,请看完整的。我的问题是一份声明。具体地说,我有一个如下所示的类(我在这里不包括完整的类定义,但如果有人认为这会有帮助,我可以这样做): 我尝试将'resource'变量声明为'var'、'dynamic'和'ResourceList'类型,并创建了一个仅包含资源的ResourceList类 每个资源都有一个名为“resourceType”的字段,所以我还尝试创建了一个ResourceList函数,该函数根据“resourc
{
"resourceType": "Bundle",
"type": "searchset",
"entry": [
{
"resource": {
"name": "Jaba the Hutt"
"birthDate": "1980-07-27",
"id": "b26646dd-c549-4981-834e-bb4145d104b8",
"resourceType": "Patient"
}
}
]
}
如有任何建议,将不胜感激
更新我的问题。有趣的是,第一个答案与我目前的想法相似
class Bundle_Entry {
dynamic resource;
Bundle_Entry({this.resource});
Bundle_Entry.fromJson(Map<String, dynamic> json) =>
Bundle_Entry(
resource: json['resource'] == null
? null
: ResourceTypes(
json['resource']['resourceType'], json['resource'] as Map<String, dynamic>)
);}
Map<String, dynamic> toJson() => _$Bundle_EntryToJson(this);
}
dynamic ResourceTypes(String type, Map<String, dynamic> json) {
if (type == 'Element') return (new Element.fromJson(json));
if (type == 'Extension') return (new Extension.fromJson(json));
if (type == 'Patient') return (new Narrative.fromJson(json));
class Bundle\u条目{
动态资源;
Bundle_条目({this.resource});
Bundle_Entry.fromJson(映射json)=>
Bundle\u条目(
资源:json['resource']==null
无效的
:资源类型(
json['resource']['resourceType'],json['resource']作为映射)
);}
映射到JSON()=>\u$Bundle\u EntryToJson(这个);
}
动态资源类型(字符串类型,映射json){
if(type=='Element')返回(new-Element.fromJson(json));
if(type=='Extension')返回(new Extension.fromJson(json));
if(type=='Patient')返回(new-description.fromJson(json));
我的问题是,然后我必须对每个ResourceType进行硬编码,似乎应该有一种更简单的方法。我一直在尝试构建一个类似的东西。我使用继承来实现它,并创建了一个函数来基于ResourceType字段返回资源
Resource getResource(json) {
if(json == null) return null;
if(json["resourceType"] == "Patient") return Patient.fromJson(json);
if(json["resourceType"]=="Procedure") return Procedure.fromJson(json);
if(json["resourceType"]=="Encounter") return Encounter.fromJson(json);
if(json["resourceType"]=="Appointment") return Appointment.fromJson(json);
if(json["resourceType"]=="Slot") return Slot.fromJson(json);
if(json["resourceType"]=="Slot") return Slot.fromJson(json);
if(json["resourceType"]=="Observation") return Observation.fromJson(json);
if(json["resourceType"]=="Condition") return Condition.fromJson(json);
if(json["resourceType"]=="DiagnosticReport") return DiagnosticReport.fromJson(json);
if(json["resourceType"]=="MedicationRequest") return MedicationRequest.fromJson(json);
if(json["resourceType"]=="CarePlan") return CarePlan.fromJson(json);
if(json["resourceType"]=="Practitioner") return Practitioner.fromJson(json);
if(json["resourceType"]=="AllergyIntolerance") return AllergyIntolerance.fromJson(json);
return Resource.fromJson(json);
}
其中,患者、程序等是资源的子类:
class Entry {
String fullUrl;
Resource resource;
factory Entry.fromJson(Map<String, dynamic> json) => Entry(
fullUrl: json["fullUrl"] == null ? null : json["fullUrl"],
//This line is the magic
resource: getResource(json["resource"]),
search: json["search"] == null ? null : Search.fromJson(json["search"]),
response: json["response"] == null
? null
: Response.fromJson(json["response"]),
);
类条目{
字符串完整URL;
资源;
工厂条目.fromJson(映射json)=>条目(
fullUrl:json[“fullUrl”]==null?null:json[“fullUrl”],
//这句话很神奇
资源:getResource(json[“资源]),
search:json[“search”]==null?null:search.fromJson(json[“search”]),
响应:json[“响应”]==null
无效的
:Response.fromJson(json[“Response”]),
);
我一直在尝试构建一个类似的东西。我使用继承来实现它,并创建了一个函数来基于ResourceType字段返回资源
Resource getResource(json) {
if(json == null) return null;
if(json["resourceType"] == "Patient") return Patient.fromJson(json);
if(json["resourceType"]=="Procedure") return Procedure.fromJson(json);
if(json["resourceType"]=="Encounter") return Encounter.fromJson(json);
if(json["resourceType"]=="Appointment") return Appointment.fromJson(json);
if(json["resourceType"]=="Slot") return Slot.fromJson(json);
if(json["resourceType"]=="Slot") return Slot.fromJson(json);
if(json["resourceType"]=="Observation") return Observation.fromJson(json);
if(json["resourceType"]=="Condition") return Condition.fromJson(json);
if(json["resourceType"]=="DiagnosticReport") return DiagnosticReport.fromJson(json);
if(json["resourceType"]=="MedicationRequest") return MedicationRequest.fromJson(json);
if(json["resourceType"]=="CarePlan") return CarePlan.fromJson(json);
if(json["resourceType"]=="Practitioner") return Practitioner.fromJson(json);
if(json["resourceType"]=="AllergyIntolerance") return AllergyIntolerance.fromJson(json);
return Resource.fromJson(json);
}
其中,患者、程序等是资源的子类:
class Entry {
String fullUrl;
Resource resource;
factory Entry.fromJson(Map<String, dynamic> json) => Entry(
fullUrl: json["fullUrl"] == null ? null : json["fullUrl"],
//This line is the magic
resource: getResource(json["resource"]),
search: json["search"] == null ? null : Search.fromJson(json["search"]),
response: json["response"] == null
? null
: Response.fromJson(json["response"]),
);
类条目{
字符串完整URL;
资源;
工厂条目.fromJson(映射json)=>条目(
fullUrl:json[“fullUrl”]==null?null:json[“fullUrl”],
//这句话很神奇
资源:getResource(json[“资源]),
search:json[“search”]==null?null:search.fromJson(json[“search”]),
响应:json[“响应”]==null
无效的
:Response.fromJson(json[“Response”]),
);
既然你建议将此作为答案,请提供完整的代码。答案需要提供问题的可行答案。我更新了帖子。如果你想让我补充更多,请告诉我。既然你建议将此作为答案,请提供完整的代码。答案需要提供问题的可行答案。我更新了发帖。如果你想让我补充更多,请告诉我。