【问题标题】:How to deserialise JSON to FHIR valueset resource in .net如何在 .net 中将 JSON 反序列化为 FHIR 值集资源
【发布时间】:2018-03-16 06:11:15
【问题描述】:

我在将值集扩展的响应转换为 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?

【问题讨论】:

  • Hl7.Fhir.Rest 是 FHIR 的官方 .Net 库,具有反序列化以纠正 FHIR json 的方法,所以是的,我建议您看一下。
  • 感谢@MirjamBaltus,官方图书馆确实让事情变得更容易。官方图书馆真的很好。我想我最初的意图是使用 RestSharp,因为我会(可能)将它用于其他非 fhir 目的。

标签: c# hl7-fhir


【解决方案1】:

您可以使用 HttpClient 和 https://www.nuget.org/packages/Hl7.Fhir.DSTU2/(或 https://www.nuget.org/packages/Hl7.Fhir.STU3/)(或 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\0.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;
                        }
                    }

                }

            }
        }

【讨论】:

    【解决方案2】:

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

    但是我接受了@Mirjam 的建议,并改用了 Hl7.Fhir.Rest (以及来自 HL7.Fhir.Model 的 ValueSet 类 - 它包含比可以使用自定义类来实现。

    // 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

    【讨论】:

    • Matt,您会(请)将您的命名空间和 nuget 包添加到您的答案中吗?我不知道 FhirClient 来自哪里。
    • 感谢@granadaCoder。我已经提到了命名空间,但我已经编辑过,所以现在它更明显了。 (Hl7.Fhir.Rest) 并提供了完整代码的链接。我使用了您在下面链接到的 STU3 包。我在这里写了我的学习博客snoyowie.com/2018/03/exploring-fhir-value-set-expansion-in-net
    • #petPeeve。当它很明显时,“var”是可以的(对我来说) var emp = new Employee();但是当它不明显时,特别是对于代码/教学示例,我不喜欢“var”.....您能否更新您的示例以准确显示“result”的数据类型是什么......就像在“result = client.ExpandValueSet ......”这是一个很好的答案(我刚刚阅读了你的博客文章)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    相关资源
    最近更新 更多