如何把java变量放到JSON字符串中?

问题描述 投票:-1回答:1

我有一些变量,其值是动态的,但在这个例子中,我将给出每个变量的固定值。如何把变量放到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,但是没有用。谁能给我更多的建议?谢谢大家

java json web-services variables dynamic
1个回答
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中的数据填充。

© www.soinside.com 2019 - 2024. All rights reserved.