我将 Fhir-net-api 与 DSTU2 结合使用,将 JSON 对象解析为 C# 模型。一切正常,只是我无法访问资源类型 Procedure 的 Reason 元素。例如,我使用 FhirParser 将以下 JSON 对象解析为 Procedure 模型:
{
"resourceType": "Procedure",
"identifier": [
{
"system": "https://mrd2.melanoma.org.au/fhir",
"value": "100200199664802"
}
],
"subject": { "reference": "Patient/10101000001733" },
"status": "completed",
"category": {
"coding": [
{
"system": "https://mrd2.melanoma.org.au/fhir/RootType",
"code": "3004"
}
],
"text": "Primary Surgery"
},
"bodySite": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "7771000"
}
],
"text": "Left Forearm, Anterior"
}
],
"reasonReference": { "reference": "/Condition/10106000001807" },
"performedDateTime": "1968-03-11",
"report": [ { "reference": "/DiagnosticReport/100200199664828" } ]
}
并且生成的对象具有以下条目(摘录): Procedure
我可以很好地访问 Report[0].Reference
,但它不能与 Reason.Reference
一起使用。我的 JSON 对象中的数据是否有误?
我看到 Reason 是 Hl7.Fhir.Model.Element 类型,Report 是 Hl7.Fhir.Model 类型。资源引用。有没有办法将 Reason 更改为 Hl7.Fhir.Model.ResourceReference 然后访问 Reference 元素?
如有任何提示,我们将不胜感激。谢谢。
问候,
拉米
最佳答案
如您所见,reasonReference
的类型是 Model.Element
,而 report
的类型是 ResourceReference
。这种差异源于 FHIR specification for Procedure 中这些元素的定义。 ,其中 report
固定为 Reference
类型,但 reason
(或者更确切地说 reason[x]
)可以是CodeableConcept
或 Reference
。
当元素可以是多种类型时(我们称之为“选择元素”,您可以识别它们,因为它们的名称在规范中以 [x]
结尾),我们创建了一个 C# Model.Element
类型的成员(Reference 和 CodeableConcept 的基类)。
现在,根据您刚刚解析或接收的实例,reason
成员的内容可以是这两种类型之一。因此,您必须检查您的代码:
if(Reports[0].reason is ResourceReference)
{
var reference = (ResourceReference)Reports[0].reason;
//handle the case where this is a reference
//doing reference.Reference will now work as expected
}
else if(Reports[0].reason is CodeableConcept)
{
var concept = (CodeableConcept)Reports[0].reason;
//handle the case where this is a codeable concept
}
else
{
// Oops! Should not happen unless the standard has changed
}
当然,如果你确定你只能接收reason是ResourceReference的实例,你可以直接做一个cast:
var myReference = (ResourceReference)Reports[0].Reference;
// myReference.Reference and myReference.Display will now work
关于c# - 无法访问 FHIR 模型中的 Procedure.reason,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302188/