以下是代码:
String str="{\"code\":\"na7ne\",\"identity\":1671760031645,\"password\":\"88888888\",\"username\":\"fuj\"}"; JSONObject jsonObject = JSON.parseObject(str); System.out.println(str); for(Map.Entry<String,Object> map: jsonObject.entrySet()){ if(map.getKey().contains("password")) JSONPath.set(jsonObject,"$."+map.getKey(),"***"); } System.out.println(jsonObject.toJSONString()); 以下是输出内容: {"code":"na7ne","identity":1671760031645,"password":"88888888","username":"fuj"} {"password":"***","code":"na7ne","identity":1671760031645,"username":"fuj"}