Java 简单 json 不替换属性值

Java Simple json not replacing the value of a attribute

正如上面的标题所述,我正在尝试将“name”的值替换为“abc”,但这似乎不是使用 Simple Json java 代码覆盖它,因为它是相同的值。

这是我的 java 代码:

    String jsonString = 
    "{"
        + "\"data\":"
        + "["
            + "{"
                + "\"jazz\":\"black\","
                + "\"name\":\"white\","
                + "\"given\":\"red\","
                + "\"sam\":\"blue\","
                + "\"mail\":\"yellow\","
                + "\"member\":\"green\","
                + "\"department\":\"green\","
                + "\"title\":\"green\""
            + "}"
        + "]"
    + "}";
    JSONParser parser = new JSONParser();
    JSONObject jsonObj = (JSONObject) parser.parse(jsonString);
    JSONObject newJSON = new JSONObject();

    jsonObj.remove("name");
    jsonObj.put("name", "abc");

正如我所说,上面的代码似乎对 json 结构中已有的“name”属性没有任何作用。上面的输出如下所示:

{
    "data": [
        {
            "given": "red",
            "mail": "yellow",
            "jazz": "black",
            "name": "white",
            "member": "green",
            "department": "green",
            "title": "green",
            "sam": "blue"
        }
    ],
    "name": "abc"
}

输出应该是这样的:

{
    "data": [
        {
            "given": "red",
            "mail": "yellow",
            "jazz": "black",
            "name": "abc",
            "member": "green",
            "department": "green",
            "title": "green",
            "sam": "blue"
        }
    ]
}

知道为什么不更改它吗?

更新 1

你体内有物体 json 物体 您需要获取内部数据对象并对其进行修改 jsonObj.get("data").put("name", "abc")

这对我有用:

JSONArray jsonArray = (JSONArray)jsonObj.get("data");
    JSONObject jsonObject = ((JSONObject)(jsonArray).get(0));
    jsonObject.put("name", "abc");
    System.out.println(jsonObj.toJSONString());

您有时会遇到以灵活的方式替换某些值是完美的情况。所以我想使用 json-path 依赖性来展示这种额外的方法。

指定路径集合替换真实数据,例如:

import static com.google.common.collect.Lists.newArrayList;

...

    private static final List<String> PATHS_TO_REPLACE = newArrayList(
            "$.email",
            "$.colleagues.[*].email",
            "$.other.required.pathmask"
    );

最重要的代码部分:

    public String maskSensitiveData(String asJson) {
        DocumentContext parsed = JsonPath.parse(asJson);
        PATHS_TO_REPLACE.forEach(path -> parsed.set(path, "***starred***"));
        return parsed.jsonString();
    }

为了避免 com.jayway.jsonpath.PathNotFoundException 如果你确定它们必须被抑制,你可以使用特殊配置:

    private static final Configuration CONFIGURATION = Configuration
            .builder()
            .options(Option.SUPPRESS_EXCEPTIONS)
            .build();

parsed文档应以更新的方式提供:

    DocumentContext parsed = JsonPath.using(CONFIGURATION).parse(asJson);

要使用代码,我建议尝试 prepared test for correspond service

P.S.

如果你想以动态方式计算设置值的星星(或仅隐藏部分数据),它也可以处理。为了保持数据数组的简单,请注意同一对象的 map 方法。对应的例子也加入了service:

    public String flexibleMaskingSensitiveData(String asJson) {
        DocumentContext parsed = JsonPath.using(CONFIGURATION).parse(asJson);
        PATHS_TO_REPLACE.forEach(path -> parsed.map(path,
                (currentValue, conf) -> starringCurrentValue(currentValue)));
        return parsed.jsonString();
    }

    private Object starringCurrentValue(Object currentValue) {
        return ofNullable(currentValue)
                .filter(String.class::isInstance)
                .map(String.class::cast)
                .map(String::length)
                .map(times -> StringUtils.repeat('*', times))
                .orElse("");
    }