jar:fastjson-1.2.31.jar
类名:com.alibaba.fastjson.JSONArray.class
目前就使用到2个转换,前一个参数传json串,后一个参数传实体类的class
json串转java对象,JSONArray.parseObject(text, clazz);
json串转java List,JSONArray.parseArray(text, clazz);
转换的时候最好用try catch包着,进行转换错误处理。
附上示例代码:
单个对象转换:
User user = null;
try {
user = JSONArray.parseArray(userJson, User.class);
}catch (Exception e) {
logger.error("转换用户json数据出错"+ e.getMessage()+",json:"+userJson, e);
}
list转换:
List<User> userList = null;
try {
userList = JSONArray.parseArray(userJson, User.class);
}catch (Exception e) {
logger.error("转换用户json数据出错"+ e.getMessage()+",json:"+userJson, e);
}