如何将 JSON 反序列化为 .net 中的 FHIR 值集资源

How to deserialise JSON to FHIR valueset resource in .net

我在将值集扩展的响应转换为 c# 中的值集资源对象时遇到问题。

我目前正在使用 RestSharp,REST 调用成功,以下输出预期的 JSON。

IRestResponse response = client.Execute(request);
var result = JsonConvert.DeserializeObject(response.Content);
Console.WriteLine(result);

我试过了

var result = JsonConvert.DeserializeObject<ValueSet>(response.Content);

但是它产生了一个空对象。我确定我犯了一些菜鸟错误,也许应该考虑使用 Hl7.Fhir.Rest 而不是 RestSharp?

所以我最终能够通过创建自定义 ValueSet class 来反序列化 RestSharp JSON 响应(我刚刚在我的实验中使用了 http://json2csharp.com/)。

但是我采纳了@Mirjam 的建议,并改用了 Hl7.Fhir.Rest(以及来自 HL7.Fhir.Model[ 的 ValueSet class - 比使用自定义 class.

包含的优点多得多
// using using Hl7.Fhir.Model;
// using Hl7.Fhir.Rest;

const string Endpoint = "https://ontoserver.csiro.au/stu3-latest";
var client = new FhirClient(Endpoint);

//uri for the value set to be searched, and text filter         
var filter = new FhirString("inr");
var vs_uri = new FhirUri("http://snomed.info/sct?fhir_vs=refset/1072351000168102");

ValueSet result = client.ExpandValueSet(vs_uri, filter);
 
//Write out the display term of the first result.
Console.WriteLine(result.Expansion.Contains.FirstOrDefault().Display);

还有一些其他方法支持额外的参数...

代码可用 - https://gist.github.com/MattCordell/32f3c62b4e66bd1ecb17b65f2f498acb

您可以使用 HttpClient 和 https://www.nuget.org/packages/Hl7.Fhir.DSTU2/ (or https://www.nuget.org/packages/Hl7.Fhir.STU3/) ( or https://www.nuget.org/packages/Hl7.Fhir.R4/ )(或任何最新的)

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

/* magic ones */
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Model;

        string url = "https://www.somebody.com/FHIR/api/Patient?given=Jason&family=Smith";

        HttpClient client = new HttpClient();
        {
            using (HttpResponseMessage response = await client.GetAsync(url))
            {
                using (HttpContent content = response.Content)
                {
                    string responseString = await content.ReadAsStringAsync();
                    response.EnsureSuccessStatusCode();

                    /* Hl7.Fhir.DSTU2  \.nuget\packages\hl7.fhir.dstu2[=10=].96.0 */

                    FhirJsonParser fjp = new FhirJsonParser(); /* there is a FhirXmlParser as well */
                    /* You may need to Parse as something besides a Bundle depending on the return payload */
                    Hl7.Fhir.Model.Bundle bund = fjp.Parse<Hl7.Fhir.Model.Bundle>(responseString);
                    if (null != bund)
                    {
                        Hl7.Fhir.Model.Bundle.EntryComponent ec = bund.Entry.FirstOrDefault();
                        if (null != ec && null != ec.Resource)
                        {
                            /* again, this may be a different kind of object based on which rest url you hit */
                            Hl7.Fhir.Model.Patient pat = ec.Resource as Hl7.Fhir.Model.Patient;
                        }
                    }

                }

            }
        }