英文:
Parsing Fhir json response into a simple object
问题
Here is the translated code snippet you provided:
var response = await _fhirClient.OperationAsync(new Uri(EndPointUrl + "FHIR/STU3/ReferralRequest"), "ers.fetchworklist", paramResource);
var eReferrals = new List<eReferral>();
if (response != null)
{
var patientNode = FhirJsonNode.Parse(await response.ToJsonAsync());
var entries = patientNode.Children("entry").ToList();
foreach (var entry in entries)
{
var extensions = entry.Children("extension").ToList();
// Extracting relevant data
var priorityUrl = extensions[0]["url"]?.ToString();
var priorityValue = extensions[0]["valueCodeableConcept"]["coding"][0]["code"]?.ToString();
var specialtyUrl = extensions[1]["url"]?.ToString();
var specialtyValue = extensions[1]["valueCodeableConcept"]["coding"][0]["code"]?.ToString();
// You can continue extracting other fields in a similar manner
eReferrals.Add(new eReferral
{
Priority = priorityValue,
Specialty = specialtyValue,
// Add other properties here
});
}
}
return eReferrals;
This code parses the JSON response and extracts the "priority" and "specialty" values for each entry into a list of eReferral
objects. You can follow a similar approach to extract other elements you need.
英文:
Hi I've been trying to fathom this all day and frankly it's beyond me.
So I've got some json response from a firely c# call and I want to get certain elements into a nice list of objects that are easier to handle and display the data back to the user.
My object
public class eReferral
{
public string Reference { get; set; }
public string Priority { get; set; }
public string Specialty { get; set; }
public string Patient { get; set; }
}
The json
{
"meta": {
"profile": [
"https://fhir.nhs.uk/STU3/StructureDefinition/eRS-FetchWorklist-List-1"
]
},
"resourceType": "List",
"contained": [
{
"id": "Practitioner-021600556514",
"meta": {
"profile": [
"https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Practitioner-1"
]
},
"resourceType": "Practitioner",
"identifier": [
{
"system": "http://fhir.nhs.net/Id/sds-user-id",
"value": "021600556514"
}
]
},
{
"id": "Patient-9462979626",
"meta": {
"profile": [
"https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Patient-1"
]
},
"resourceType": "Patient",
"identifier": [
{
"system": "http://fhir.nhs.net/Id/nhs-number",
"value": "9462979626"
}
]
},
{
"id": "Patient-1000000001",
"meta": {
"profile": [
"https://fhir.nhs.uk/STU3/StructureDefinition/eRS-Patient-1"
]
},
"resourceType": "Patient",
"identifier": [
{
"system": "http://fhir.nhs.net/Id/nhs-number",
"value": "1000000001"
}
]
}
],
"status": "current",
"mode": "snapshot",
"entry": [
{
"extension": [
{
"extension": [
{
"url": "priority",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-Priority-1",
"code": "ROUTINE"
}
]
}
},
{
"url": "specialty",
"valueCodeableConcept": {
"coding": [
{
"system": "_baseUrl_/STU3/CodeSystem/SPECIALTY",
"code": "CARDIOLOGY"
}
]
}
},
{
"url": "patient",
"valueReference": {
"reference": "#Patient-9462979626"
}
},
{
"url": "eReferralPathwayStart",
"valueDateTime": "2021-09-03T14:09:15.297Z"
},
{
"url": "clinicalInfoPrinted",
"valueBoolean": false
},
{
"url": "requestContextStatus",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-RequestContextStatus-1",
"code": "NEVER_REVIEWED",
"display": "Never Reviewed"
}
]
}
},
{
"url": "clinicalInfoFirstSubmitted",
"valueDateTime": "2021-09-03T14:09:15.297Z"
},
{
"url": "referralType",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-ReferralType-1",
"code": "TRIAGE_DEFERRAL"
}
]
}
},
{
"url": "service",
"valueReference": {
"identifier": {
"system": "http://fhir.nhs.net/Id/ers-service",
"value": "11012"
},
"display": "Excellent Cardiology Business Service 12"
}
}
],
"url": "https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1"
}
],
"item": {
"reference": "ReferralRequest/000000070002"
}
},
{
"extension": [
{
"extension": [
{
"url": "priority",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-Priority-1",
"code": "ROUTINE"
}
]
}
},
{
"url": "specialty",
"valueCodeableConcept": {
"coding": [
{
"system": "_baseUrl_/STU3/CodeSystem/SPECIALTY",
"code": "CARDIOLOGY"
}
]
}
},
{
"url": "patient",
"valueReference": {
"reference": "#Patient-1000000001"
}
},
{
"url": "allocatedClinician",
"valueReference": {
"reference": "#Practitioner-021600556514"
}
},
{
"url": "eReferralPathwayStart",
"valueDateTime": "2021-09-03T14:09:11.381Z"
},
{
"url": "clinicalInfoPrinted",
"valueBoolean": false
},
{
"url": "requestContextStatus",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-RequestContextStatus-1",
"code": "NEVER_REVIEWED",
"display": "Never Reviewed"
}
]
}
},
{
"url": "clinicalInfoFirstSubmitted",
"valueDateTime": "2021-09-03T14:09:11.022Z"
},
{
"url": "clinicalInfoLastUpdated",
"valueDateTime": "2021-09-03T14:09:12.573Z"
},
{
"url": "appointmentStart",
"valueDateTime": "2021-09-05T15:09:03.971Z"
},
{
"url": "referralType",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/STU3/CodeSystem/eRS-ReferralType-1",
"code": "APPOINTMENT"
}
]
}
}
],
"url": "https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1"
}
],
"item": {
"reference": "ReferralRequest/000000070001"
}
}
]
}
Currently I've got this.
var response = await _fhirClient.OperationAsync(new Uri(EndPointUrl + "FHIR/STU3/ReferralRequest"), "ers.fetchworklist", paramResource);
var eReferals = new List<eReferral>();
if (response != null)
{
var patientNode = FhirJsonNode.Parse(await response.ToJsonAsync());
var use = patientNode.Children("entry").Children("extension").ToList();
foreach(var x in use)
{
eReferals.Add(new eReferral
{
Priority = x["extension[0]"]
})
}
}
return eReferals;
However I don't know how to referance and get the specific elements e.g "url" : "priority" and get the object at that point.
I might be doing this all wrong and there is a much better way to get the various elements however I've been round the block trying to do this - with a few methods and I'm non the wiser at the end.
答案1
得分: 1
您的操作调用结果是一个包含(引用)ReferralRequest资源的List资源。每个条目似乎都有一个包含有关转诊请求的更多详细信息的复杂扩展,并且根据您的问题/示例,看起来您希望创建具有ReferralRequest的引用值以及扩展中的Patient、专科和优先级详情的eReferral对象。
如果这是正确的假设,我建议与List资源结构一起工作,而不是将其转换为json,然后再转换回结构。当您了解List模型时,可以访问其属性,包括扩展。以下是我提取您提到的值的示例:
List list = await _fhirClient.OperationAsync(new Uri(EndPointUrl + "FHIR/STU3/ReferralRequest"), "ers.fetchworklist", paramResource);
if (list != null)
{
foreach (List.EntryComponent e in list.Entry)
{
Extension mainExtension = e.GetExtension("https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1");
eReferals.Add(new eReferral
{
Reference = e.Item.Reference,
Priority = mainExtension.GetExtensionValue<CodeableConcept>("priority").Coding.First().Code,
Specialty = mainExtension.GetExtensionValue<CodeableConcept>("specialty").Coding.First().Code,
Patient = mainExtension.GetExtensionValue<ResourceReference>("patient").Reference
}) ;
}
}
我还想补充一句,但不知道您是否对此有任何控制:似乎对于Patient资源,NHS号码已集成到技术ID中。将个人身份信息添加到FHIR资源的技术标识中是不鼓励的。
英文:
Your operation call results in a List resource, where the entries are (references to) ReferralRequest resources. Each entry seems to have a complex extension with more details about the referral request, and from your question/example it looks like you want to create eReferral objects that have the reference value of the ReferralRequest as well as the details for the Patient, specialty and priority from the extension.
If that is the correct assumption, what I would advice is to work with the List resource structure, instead of converting it to json and then back to a structure. When you know the List model, you can access its properties including the extensions. Here's an example where I extract the values that you mentioned:
List list = await _fhirClient.OperationAsync(new Uri(EndPointUrl + "FHIR/STU3/ReferralRequest"), "ers.fetchworklist", paramResource);
if (list != null)
{
foreach (List.EntryComponent e in list.Entry)
{
Extension mainExtension = e.GetExtension("https://fhir.nhs.uk/STU3/StructureDefinition/Extension-eRS-ReferralsforReview-WorkListItem-1");
eReferals.Add(new eReferral
{
Reference = e.Item.Reference,
Priority = mainExtension.GetExtensionValue<CodeableConcept>("priority").Coding.First().Code,
Specialty = mainExtension.GetExtensionValue<CodeableConcept>("specialty").Coding.First().Code,
Patient = mainExtension.GetExtensionValue<ResourceReference>("patient").Reference
}) ;
}
}
I do want to add an extra remark, but do not know if you have any control over this: it seems that for the Patient resources, the NHS number is integrated into the technical id. This kind of adding PII to technical identities of FHIR resources is highly discouraged.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论