如何将Java变量放入JSON字符串?

时间:2020-05-18 01:45:18

标签: java json web-services variables dynamic

我有一些变量,该值是动态的,但在此示例中,我将给出每个变量的固定值。如何将变量放入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 Object put,但是它不起作用。谁能提供更多建议?谢谢

1 个答案:

答案 0 :(得分:0)

下面是一个简单的示例,说明如何从文件读取JSON,修改字段并将更新的JSON打印到控制台:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GsonMain {

    public static void main(String[] args) throws IOException {
        Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .create();
        byte[] jsonBytes = Files.readAllBytes(Paths.get("./src/main/java/gson/data.json"));
        String json = new String(jsonBytes);

        Map<String,Object> result = gson.fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());

        String date = java.time.Clock.systemUTC().instant().toString();

        List<Map<String,Object>> entry = (List<Map<String,Object>>) result.get("entry");
        for(Map<String,Object> item: entry){
            Map<String,Object> resource = (Map<String, Object>) item.get("resource");
            resource.put("created", java.time.Clock.systemUTC().instant().toString());
        }

        System.out.println(gson.toJson(result));
    }
}

我将您的JSON字符串用作“ data.json”文件内容。输出为:

{
  "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": "2020-05-18T02:17:05.274Z",
        "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": "2020-05-18T02:17:05.274Z",
        "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
          }
        ]
      }
    }
  ]
}

请注意,条目>资源>创建的字段现在如何包含时间戳。您可以使用此逻辑作为模板来填充其他字段。这只是一个简单的示例,还有其他方法可以执行此操作,例如创建一个或多个POJO类来表示数据,并使用Gson创建用JSON数据填充的类的实例。