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

时间:2018-03-16 06:11:15

标签: c# hl7-fhir

我无法将值集扩展的响应转换为c#中的valueset资源对象。

我目前正在使用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?

2 个答案:

答案 0 :(得分:0)

所以我最终能够通过创建自定义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");

var 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

答案 1 :(得分:0)

您可以使用HttpClient和https://www.nuget.org/packages/Hl7.Fhir.DSTU2/(或https://www.nuget.org/packages/Hl7.Fhir.STU3/

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
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;
                        }
                    }

                }

            }
        }