hapi-fhir将fhir对象转换为json字符串

时间:2017-05-25 01:57:53

标签: java hapi-fhir

我目前正在使用hapi-fhir 2.3用于DSTU2。如果我使用hapi-fhir客户端api创建了一个RiskAssessment对象。如何将其转换为标准的fhir json格式?

如果使用toSting()方法,则会生成以下结果:

{
"mySubject" : -{
"myReference" : -{
"myHaveComponentParts" : false,
"myUnqualifiedId" : 005543907A,
"myCoercedValue" : 005543907A,
"myStringValue" : 005543907A
}
},
"myDate" : -{
"myFractionalSeconds" : 575,
"myPrecision" : SECOND,
"myTimeZone" : -{
"rawOffset" : 28800000,
"rawOffsetDiff" : 0,
"checksum" : -305565502,
"dstSavings" : 0,
"transitions" : -[
-9048018124800000,
-8918966038528000,
-3823593062399950,
-3781140480000000,
-3722379263999950,
-3651969024000000,
2111569920000050,
2158623129600000,
2232955699200050,
2287440691200000,
2361773260800050,
2416258252800000,
2493068083200050,
2547553075200000,
2621885644800050,
2676370636800000,
2750703206400050,
2805188198400000,
8660385792000000
],
"offsets" : -[
28800000,
29143000,
32400000,
3600000
],
"willGMTOffsetChange" : false,
"ID" : Asia/Shanghai
},
"myTimeZoneZulu" : false,
"myCoercedValue" : May 25, 2017 9:46:01 AM,
"myStringValue" : 2017-05-25T09:46:01+08:00
},
"myMethod" : -{
"myText" : -{
"myCoercedValue" : My risk score engine.,
"myStringValue" : My risk score engine.
}
},
"myPrediction" : -[
-{
"myOutcome" : -{
"myText" : -{
"myCoercedValue" : Remission,
"myStringValue" : Remission
}
},
"myRelativeRisk" : -{
"myCoercedValue" : 0.6949999928474426,
"myStringValue" : 0.6949999928474426
}
}
]
}

我期待的是像

{
  "resourceType": "RiskAssessment",
  "id": "prognosis",
  "text": {
    "status": "additional",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n      <p>Moderate risk of permanent weakness of the left arm, but otherwise no permanent disability</p>\n    </div>"
  },
  "status": "final",
  "occurrenceDateTime": "2010-11-22",
  "condition": {
    "reference": "Condition/stroke",
    "display": "Ischemic Stroke"
  },
  "prediction": [
    {
      "outcome": {
        "coding": [
          {
            "system": "http://snomed.info/sct",
            "code": "249943000:363698007=72098002,260868000=6934004"
          }
        ],
        "text": "permanent weakness of the left arm"
      },
      "qualitativeRisk": {
        "coding": [
          {
            "system": "http://hl7.org/fhir/risk-probability",
            "code": "moderate",
            "display": "moderate likelihood"
          }
        ]
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:1)

String riskAssessmentJson = FhirContext.forDstu2().newJsonParser().encodeResourceToString(resource);

答案 1 :(得分:0)

查看文档HAPI-FHIR部分&#34;将资源编码为字符串&#34;。 但你真的需要JSON表示吗? HAPI客户端在发送到服务器之前自动将对象(资源)解析为JSON,在服务器端,它们又是对象。