将json投射到hapi fhir对象

时间:2016-08-03 15:02:20

标签: gson hl7-fhir hapi hapi-fhir hapi-fhir-android-library

我有一个约会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类

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

但它为约会对象提供空字段

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

不是直接使用GSON,最好使用HAPI FHIR api,它在内部使用GSON来解析JSON。 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);