替换字符串中的值特定键

时间:2018-06-27 08:05:04

标签: java json

我在表item_action_info的datasets_detail列中存储了以下字符串:

[{
    "datasetAttributeId": 5,
    "nextUpdate": 1587546084,
    "lastSuccessfulRefreshTime": 1587546084,
    "refreshActionId": 2,
    "errorCode": 0,
    "isMFAInstanceRefreshRequired": false
}, {
    "datasetAttributeId": 6,
    "nextUpdate": 1587546084,
    "lastSuccessfulRefreshTime": 1587546084,
    "refreshActionId": 2,
    "errorCode": 0,
    "isMFAInstanceRefreshRequired": false
}, {
    "datasetAttributeId": 8,
    "nextUpdate": 1587546084,
    "lastSuccessfulRefreshTime": 1587546084,
    "refreshActionId": 2,
    "errorCode": 0,
    "isMFAInstanceRefreshRequired": false
}]

我希望为每次出现的“ lastSuccessfulRefreshTime”替换值。 我该如何用Java做? 下面是示例代码。请建议完成相同操作。

String strJson =CommonUtils.clobStringConversion(resultSet.getClob("DATASETS_DETAILS"));

//在此处完成其余代码

1 个答案:

答案 0 :(得分:0)

如果要将其作为JSON对象进行操作,则可以通过Google GSON进行。

String strJson =CommonUtils.clobStringConversion(resultSet.getClob("DATASETS_DETAILS"));

Gson gson = new Gson();
List<StringMap> parsedObject = new Gson().fromJson(strJson, List.class);

// Final String
String newReplacedString = gson.toJson(parsedObject.stream().map((item) -> {
    // Put your new value here
    item.put("lastSuccessfulRefreshTime", "Replaced Value");
    return item;
}).collect(Collectors.toList()));