zoukankan      html  css  js  c++  java
  • 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()); //JSONString
      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();
        }
  • 相关阅读:
    python系列十二:python3模块
    python系列十一:python3数据结构
    python系列十:python3函数
    python系列九:python3迭代器和生成器
    python系列八:Python3条件控制&循环语句
    python系列七:Python3字典dict
    python系列六:Python3元组tuple
    Linux Ubuntu 安装SSH服务
    Linux Ubuntu 查看IP
    Linux 基础命令
  • 原文地址:https://www.cnblogs.com/mingyue1818/p/5949147.html
Copyright © 2011-2022 走看看