One. json to list
Lite
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);
}
The difference between 1 and 2 needs to use the first type two if you want to convert it into an object
. json to object
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;
}
three. Object to json
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(new BoxConfig());
four. json string to map
String expected = "{\"name\":\"Test\"}";
Map userMap = mapper.readValue(expected, Map.class);
Fives. json string to object array 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);
six. json string to Map array 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);
Seven. Jackson converts objects to LinkedHashMap by default:
String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
Eight. How to convert between json string and list or 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);
nine. Convert bean to 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>>>(){});