如何将java变量放入JSON字符串中?
我有一些变量,值是动态的,但在这个例子中,我会给出每个变量的固定值。如何将变量放入JSON字符串并将其发布到web服务
String date = java.time.Clock.systemUTC().instant().toString();
String uuid1 = "6f0be90b-db0d-1b03-8a14-aaa70a5d239d";
String uuid2 = "671d2db0-b0d3-2745-9f76-52c22255a4b4";
String val ="1589430997009_658";
这是JSON字符串,我如何用我拥有的变量替换currentDateTime、uuid1、uuid2和urlDocument
{
"resourceType": "Bundle",
"type": "transaction",
"meta": {
"lastUpdated": "2018-04-13T08:29:35",
"profile": [
"http://ihe.net/fhir/tag/iti-65"
]
},
"entry": [
{
"resource": {
"resourceType": "DocumentManifest",
"type": {
"coding": [
{
"code": "MPQ",
"display": "MPQ",
"system": "2.16.840.1.113883.5.25"
}
]
},
"status": "current",
"id": "1231010",
"description": "DocumentManifest",
"created": "{{currentDateTime}}",
"masterIdentifier": {
"system": "1.1.1.9.9",
"value": "{{uuid1}}"
},
"content": [
{
"pReference": {
"reference": "DocumentReference/170013"
}
}
],
"subject": {
"reference": "http://xxxx-myedu.online:8081/DataGateway/fhir/dstu3/Patient/1.1.1.99.1.-.d8a08ddc-b1f1-4c86-9bc6-9203914d9e6c"
},
"source": "1.1.1"
}
},
{
"resource": {
"resourceType": "DocumentReference",
"id": "170013",
"status": "current",
"created": "{{currentDateTime}}",
"indexed": "{{currentDateTime}}",
"description": "XXXS_Softpath Report histo",
"masterIdentifier": {
"system": "urn:oid:1.2.3",
"value": "{{uuid2}}"
},
"content": [
{
"attachment": {
"contentType": "application/pdf",
"url": "{{documentUrl}}"
},
"format": [
{
"system": "DEMO_FORMATCODES",
"code": "image",
"display": "DEMO_FORMATCODES"
}
]
}
],
"context": {
"period": {
"start": "{{currentDateTime}}",
"end": "{{currentDateTime}}"
},
"facilityType": {
"coding": [
{
"system": "DEMO_HEALTHCAREFACILITYTYPECODES",
"code": "MP",
"display": "DEMO_HEALTHCAREFACILITYTYPECODES"
}
]
},
"practiceSetting": {
"coding": [
{
"system": "DEMO_PRACTICESETTINGCODES",
"code": "1200",
"display": "DEMO_PRACTICESETTINGCODES"
}
]
},
"sourcePatientInfo": {
"reference": "#thepatient"
}
},
"type": {
"coding": [
{
"system": "DEMO_TYPECODES",
"code": "11111-7",
"display": "DEMO_TYPECODES"
}
]
},
"class": {
"coding": [
{
"system": "DEMO_CLASSCODES",
"code": "11111-1",
"display": "DEMO_CLASSCODES"
}
]
},
"language": "en_US",
"securityLabel": [
{
"coding": [
{
"system": "DEMO_CONFIDENTIALITYCODES",
"code": "N",
"display": "DEMO_CONFIDENTIALITYCODES"
}
]
}
],
"author": [
{
"reference": "#1235555"
}
],
"custodian": [
{
"reference": "#1236666"
}
],
"subject": {
"reference": "http://xxx-mye.online:8081/DataGateway/fhir/dstu3/Patient/1.1.1.99.1.-.d8a08ddc-b1f1-4c86-9bc6-9203914d9e6c"
},
"contained": [
{
"resourceType": "Practitioner",
"id": "1235555",
"name": [
{
"given": [
"Support"
],
"family": [
"Administrator"
]
}
]
},
{
"resourceType": "Organization",
"id": "1236666",
"name": "XXX-MOL",
"identifier": [
{
"use": "official",
"value": "1.1.1.9.9"
}
]
},
{
"resourceType": "Patient",
"id": "#thepatient",
"identifier": [
{
"use": "temp",
"system": "urn:oid:1.1.1.99.1",
"value": "d8a08ddc-b1f1-4c86-9bc6-9203914d9e6c"
},
{
"use": "usual",
"system": "urn:oid:1.1.1.2.2.1",
"value": "PatID-101"
}
],
"name": [
{
"family": "Family-101",
"given": [
"Given-101"
]
}
],
"gender": "male",
"birthDate": "2020-01-01",
"deceasedBoolean": false
}
]
}
}
]
}
我已经尝试过使用String java中的replace函数替换它,也尝试过使用JSON对象put,但它不起作用。有人能给我更多建议吗?谢谢
# 1 楼答案
下面是一个简单的示例,演示如何从文件中读取JSON,修改字段,并将更新后的JSON打印到控制台:
我使用您的JSON字符串作为“data.JSON”文件内容。以下是输出:
请注意条目>;资源>;创建的字段现在包含一个时间戳。您可以使用此逻辑作为模板来填充其他字段。这只是一个简单的例子,还有其他方法可以做到这一点,比如创建一个或多个POJO类来表示您的数据,并使用Gson创建用JSON中的数据填充的类的实例