将 json 投射到 hapi fhir 对象

Cast json to hapi fhir object

我有一个约会 json,我需要将其转换为 DSTU2 HAPI FHIR json 对象。任何可用的标准库? Google 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 class 我使用

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

但它给出了空字段的约会对象

您可以只使用 HAPI 内置的 parser/serializer 功能:

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

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

与其直接使用 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);