【问题标题】:Cast json to hapi fhir object将 json 转换为 hapi fhir 对象
【发布时间】:2016-08-03 15:02:20
【问题描述】:

我有一个约会 json,我需要将其转换为 DSTU2 HAPI FHIR json 对象。任何可用于相同的标准库?谷歌 gson 库有效,但没有为字段中的对象赋予价值

{
  "resourceType": "Appointment",
  "id": "",
  "status": "proposed",
  "reason": {
    "text": "Regular checkup"
  },
  "description": "",
  "slot": [
    {
      "reference": "bfgf5dfdf4e45g"
    }
  ],
  "comment": "Regular yearly visit",
  "participant": [
    {
      "actor": {
        "reference": "9sdfsndjkfnksdfu3yyugbhjasbd"
      },
      "required": "required"
    },
    {
      "actor": {
        "reference": "78hjkdfgdfg223vg"
      },
      "required": "required"
    },
    {
      "actor": {
        "reference": "sdfs3df5sdfdfgdf"
      },
      "required": "required"
    }
  ]
}

需要将上面的 json 转换为我使用的 ca.uhn.fhir.model.dstu2.resource.Appointment 类

Appointment appointment = new Gson().fromJson(map.get("appointment"), Appointment.class);

但它提供了带有空字段的约会对象

【问题讨论】:

  • 预期的最终结果是什么 - 拥有一个包含所有字段的约会类?然后创建 Appointment 类并填充字段。它可能需要不止一行代码,但至少你实现了你所需要的。

标签: gson hl7-fhir hapi hapi-fhir


【解决方案1】:

与其直接使用 GSON,不如使用内部使用 GSON 解析 JSON 的 HAPI FHIR api。 Maven 依赖:

<dependency>
   <groupId>ca.uhn.hapi.fhir</groupId>
   <artifactId>hapi-fhir-base</artifactId>
   <version>2.1</version>
</dependency>
<dependency>
   <groupId>ca.uhn.hapi.fhir</groupId>
   <artifactId>hapi-fhir-structures-dstu3</artifactId>
   <version>2.1</version>
</dependency>

// 有关如何设置 gradle 和 maven 以将 HAPI fhir 依赖项添加到您的项目的更多详细信息,请查看http://hapifhir.io/download.html

片段:

FhirContext ourFhirCtx = FhirContext.forDstu3();
IParser parser=ourFhirCtx.newJsonParser().setPrettyPrint(true);
String string="{\"resourceType\":\"Appointment\",\"id\":\"\",\"status\":\"proposed\",\"reason\":{\"text\":\"Regular checkup\"},\"description\":\"\",\"slot\":[{\"reference\":\"bfgf5dfdf4e45g\"}],\"comment\":\"Regular yearly visit\",\"participant\":[{\"actor\":{\"reference\":\"9sdfsndjkfnksdfu3yyugbhjasbd\"},\"required\":\"required\"},{\"actor\":{\"reference\":\"78hjkdfgdfg223vg\"},\"required\":\"required\"},{\"actor\":{\"reference\":\"sdfs3df5sdfdfgdf\"},\"required\":\"required\"}]}";
Appointment parsed=parser.parseResource(Appointment.class,string);

【讨论】:

  • 很棒的sn-p。拯救了我的一天。
【解决方案2】:

您可以只使用 HAPI 中内置的解析器/序列化器功能:

String myJsonTxt = ""; // add your json here
FhirContext ctx = FhirContext.forDstu2();
Appointment app = (Appointment) ctx.newJsonParser().parseResource(myJsontxt);

另外,请检查您的 json,因为在 FHIR 中您不会添加空元素或属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多