我在将来自值集扩展的响应转换为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?
发布于 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/ )(或任何最新的)
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;
}
}
}
}
}
https://stackoverflow.com/questions/49314247
复制相似问题