Java简单json不替换属性的值

正如上面的标题所述,我试图将“ name ”的值替换为“ abc ”,但这似乎并没有覆盖它,因为使用简单 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"

        }

    ]

}

知道为什么它不改变它吗?


守着星空守着你
浏览 162回答 3
3回答

慕沐林林

这对我有用:JSONArray jsonArray = (JSONArray)jsonObj.get("data");     JSONObject jsonObject = ((JSONObject)(jsonArray).get(0));     jsonObject.put("name", "abc");     System.out.println(jsonObj.toJSONString());

慕尼黑5688855

您的对象内部有 json 对象您需要获取内部数据对象并修改它 jsonObj.get("data").put("name", "abc")

白衣染霜花

有时您可能会遇到以灵活的方式完美替换某些值的情况。所以我想展示这个使用json-path依赖项的附加方法。指定路径集合来替换真实数据,例如:import static com.google.common.collect.Lists.newArrayList;...&nbsp; &nbsp; private static final List<String> PATHS_TO_REPLACE = newArrayList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$.email",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$.colleagues.[*].email",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$.other.required.pathmask"&nbsp; &nbsp; );最重要的代码部分:&nbsp; &nbsp; public String maskSensitiveData(String asJson) {&nbsp; &nbsp; &nbsp; &nbsp; DocumentContext parsed = JsonPath.parse(asJson);&nbsp; &nbsp; &nbsp; &nbsp; PATHS_TO_REPLACE.forEach(path -> parsed.set(path, "***starred***"));&nbsp; &nbsp; &nbsp; &nbsp; return parsed.jsonString();&nbsp; &nbsp; }为了避免com.jayway.jsonpath.PathNotFoundException如果您确定必须抑制它们,您可以使用特殊配置:&nbsp; &nbsp; private static final Configuration CONFIGURATION = Configuration&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .options(Option.SUPPRESS_EXCEPTIONS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();并parsed应以更新的方式提供文件:&nbsp; &nbsp; DocumentContext parsed = JsonPath.using(CONFIGURATION).parse(asJson);要使用代码,我建议尝试为相应的服务准备测试。聚苯乙烯如果您想以动态方式计算星星以设置值(或仅隐藏部分数据),也可以处理。为了使数据数组保持简单,请注意map同一对象的方法。相应的示例也添加到服务中:&nbsp; &nbsp; public String flexibleMaskingSensitiveData(String asJson) {&nbsp; &nbsp; &nbsp; &nbsp; DocumentContext parsed = JsonPath.using(CONFIGURATION).parse(asJson);&nbsp; &nbsp; &nbsp; &nbsp; PATHS_TO_REPLACE.forEach(path -> parsed.map(path,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (currentValue, conf) -> starringCurrentValue(currentValue)));&nbsp; &nbsp; &nbsp; &nbsp; return parsed.jsonString();&nbsp; &nbsp; }&nbsp; &nbsp; private Object starringCurrentValue(Object currentValue) {&nbsp; &nbsp; &nbsp; &nbsp; return ofNullable(currentValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(String.class::isInstance)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(String.class::cast)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(String::length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(times -> StringUtils.repeat('*', times))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElse("");&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java