jackson转换常用

一。json转list

精简版

 public List<RewardWeightConfig> getRewardWeightConfig() {
    
    
        try {
    
    
            if (StringUtils.isNotBlank(config)) {
    
    
                List<RewardWeightConfig> configs = JsonUtils.fromJson(config, new TypeReference<List<RewardWeightConfig>>() {
    
    
                });
                return configs;
            }
        } catch (Exception e) {
    
    
            log.error("获取奖励权重配置list error:{}", e);
        }
        return null;
    }
 String strCoin = "[{\"coin\":50,\"weight\":10},{\"coin\":100,\"weight\":10},{\"coin\":200,\"weight\":10}]";
        try {
            if (StringUtils.isNotBlank(strCoin)){
                ObjectMapper objectMapper = new ObjectMapper();
                JavaType jt = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, CoinWeightConfig.class);
                List<CoinWeightConfig> configs = objectMapper.readValue(strCoin, jt);  //1
                List<CoinWeightConfig> configs2 = objectMapper.readValue(strCoin, List.class); // 2
                System.out.println(12);
            }
        }catch (Exception e){
            log.error("获取金币权重配置list error:{}",e);
        }

1和2的不同 如果想转成对象需使用第一种
在这里插入图片描述
二。json转对象

 public BoxConfig getBoxConfig() {
        try {
            if (StringUtils.isNotBlank(config)){
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(config, BoxConfig.class);
            }
        }catch (Exception e){
            log.error("获取宝箱配置error:{}",e);
        }
        return null;
    }

三。对象转json

  ObjectMapper mapper = new ObjectMapper();
        mapper.writeValueAsString(new BoxConfig());

四。json字符串转Map

String expected = "{\"name\":\"Test\"}";
Map userMap = mapper.readValue(expected, Map.class);

五。json字符串转对象数组List

String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class);
List<User> userList = mapper.readValue(expected, listType);

六。json字符串转Map数组List<Map<String,Object>>

String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class);
List<Map<String,Object>> userMapList = mapper.readValue(expected, listType);

七。jackson默认将对象转换为LinkedHashMap:

String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
ArrayList arrayList = mapper.readValue(expected, ArrayList.class);

八。json字符串与list或map互转的方法

ObjectMapper objectMapper = new ObjectMapper();
 //遇到date按照这种格式转换
 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 objectMapper.setDateFormat(fmt);
 
  String preference = "{name:'侯勇'}";
        //json字符串转map
  Map<String, String> preferenceMap = new HashMap<String, String>();
  preferenceMap = objectMapper.readValue(preference, preferenceMap.getClass());
  
  //map转json字符串
  String result=objectMapper.writeValueAsString(preferenceMap);

九。bean转换为map

List<Map<String,String>> returnList=new ArrayList<Map<String,String>>();
List<Menu> menuList=menuDAOImpl.findByParentId(parentId);
ObjectMapper mapper = new ObjectMapper();
//用jackson将bean转换为map
returnList=mapper.convertValue(menuList,new TypeReference<List<Map<String, String>>>(){});

猜你喜欢

转载自blog.csdn.net/My_Way666/article/details/106059909