首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在.net中将JSON反序列化为fhir值集资源

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

Stack Overflow用户
提问于 2018-03-16 14:11:15
回答 1查看 4.6K关注 0票数 1

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

我目前使用的是RestSharp,REST调用成功,下面的代码输出了预期的JSON。

代码语言:javascript
复制
IRestResponse response = client.Execute(request);
var result = JsonConvert.DeserializeObject(response.Content);
Console.WriteLine(result);

我试过了

代码语言:javascript
复制
var result = JsonConvert.DeserializeObject<ValueSet>(response.Content);

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-23 01:57:55

您可以使用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/ )(或任何最新的)

代码语言:javascript
复制
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;
                        }
                    }

                }

            }
        }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49314247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档