JSON 与 String、Map、JavaBean互转
JSON 与 String、Map、JavaBean互转
//解析远程登录用户信息
AttributePrincipal principal = AssertionHolder.getAssertion().getPrincipal();
if ((principal == null)|| (principal.getAttributes().isEmpty())){
log.error("远程登录接口有误,请联系开发人员!");
resp.setResult("false");
resp.setResultDesc("远程登录接口有误,请联系开发人员!");
} else {
Map attributes = principal.getAttributes();
ObjectMapper objectMapper = new ObjectMapper();
String user = (String) attributes.get("user");
user = URLDecoder.decode(user, "UTF-8");
JSONObject jsonObject = JSONObject.fromObject(userInfo); //Java Bean转JSON
JSONObject jsonuser = JSONObject.fromObject(user);
System.out.println(" ======== 2、 获取登录用户信息 userInfo =========" + jsonObject.toString()); //JSON转String
log.error(" ======== 2、 获取远程用户信息 user =========" + jsonuser.toString());
//user、partner为JSON串,解析JSON...
Map map = objectMapper.readValue(user, Map.class); //JSON转Map
String account = map.get("account")+"";
String centerCorpId = map.get("corpId")+"";
}
//String转JSON
JSONArray jsonArray = JSONArray.fromObject(params.getEmpJson());
//JSON转List
List<ActiveRange> empList = (List<ActiveRange>) jsonArray.toCollection(jsonArray, ActiveRange.class);
JSON转List:
/**
* 批量新增系统消息 -- 供其它模块调用
*/
@Override
public int insertSysNotice(List<SysNotice> list) {
if(redis.exist("dayHRO.sysNoticeList")){
String json = redis.get("dayHRO.sysNoticeList");
JSONArray jsonArray = JSONArray.fromObject(json);
List<SysNotice> list_ = (List) JSONArray.toCollection(jsonArray, SysNotice.class); //JSON转List
list.addAll(list_);
redis.set("dayHRO.sysNoticeList", redis.convertJson(list));
} else {
redis.set("dayHRO.sysNoticeList", redis.convertJson(list));
}
return list.size();
}