Java 实体转Json Json转实体 String转Json Sting转JSONObject Sting转JSONArray
·
常用:String转Json 字符串转Json
JSONObject json = JSONObject.parseObject(token);
token = json.get("token").toString();
一、实体转Json Json转实体 String转Json String转JSONArray
一、JDKjson(json-lib-2.4-jdk15.jar)
// String转Json
JSONObject json = JSONObject.fromObject(responseMsg); // JSONObject解析
// String转JSONArray
JSONArray listJson = JSONArray.fromObject(responseMsg); // JSONArray解析 "[{},{},{}]"
// 实体转Json
JSONObject json = JSONObject.fromObject(std);
// List转Json
JSONArray jsonArray = JSONArray.fromObject(list);
// Json转实体
StdCost stdTemp = (StdCost)JSONObject.toBean(json, StdCost.class);
// Json转List
List<StdCost> listTemp = JSONArray.toList(jsonArray, StdCost.class);
二、阿里云fastjson(fastjson-1.2.10.jar)
// String转Json
JSONObject json = JSONObject.parseObject(responseMsg); // JSONObject解析
// String转JSONArray
JSONArray listJson = JSONArray.parseArray(responseMsg); // JSONArray解析 "[{},{},{}]"
// 实体转Json
JSONObject json = (JSONObject)JSONObject.toJSON(std);
// List转Json
JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
// Json转实体
StdCost stdJson = JSONObject.parseObject(json.toString(), StdCost.class);
// Json转List
List<StdCost> listTemp = JSONArray.parseArray(jsonArray.toString(), StdCost.class);
完整示例:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
if(list != null && list.size() > 0){
/*for(int i=0; i<list.size(); i++){
StdCost std = list.get(i);
System.out.println("--- " + i + "/" + list.size() +
" " + std.getStudentName() +
" " + std.getStudentIdCard() +
" " + std.getFeitype() +
" " + std.getFei() +
" " + df.format(std.getCreatedDate())
);
JSONObject json = JSONObject.fromObject(std);
listJson.add(json);
StdCost stdTemp = (StdCost)JSONObject.toBean(json, StdCost.class);
System.out.println(stdTemp.getStudentName() +
" " + stdTemp.getStudentIdCard() +
" " + stdTemp.getFeitype() +
" " + stdTemp.getFei() +
" " + df.format(stdTemp.getCreatedDate())
);*/
/*JSONObject json = (JSONObject)JSONObject.toJSON(std);
listJson.add(json);
StdCost stdTemp = JSONObject.parseObject(json.toString(), StdCost.class);
System.out.println(stdTemp.getStudentName() +
" " + stdTemp.getStudentIdCard() +
" " + stdTemp.getFeitype() +
" " + stdTemp.getFei() +
" " + df.format(stdTemp.getCreatedDate())
);*/
//}
//jsonArray = JSONArray.fromObject(list);
//List<StdCost> listTemp = JSONArray.toList(jsonArray, StdCost.class);
jsonArray = (JSONArray)JSONArray.toJSON(list);
List<StdCost> listTemp = JSONArray.parseArray(jsonArray.toString(), StdCost.class);
if(listTemp != null && listTemp.size() > 0){
StdCost stdTemp = null;
for(int i=0; i<listTemp.size(); i++){
stdTemp = listTemp.get(i);
System.out.println(stdTemp.getStudentName() +
" " + stdTemp.getStudentIdCard() +
" " + stdTemp.getFeitype() +
" " + stdTemp.getFei() +
" " + df.format(stdTemp.getCreatedDate())
);
}
}
}
二、String转Json(Sting转JSONObject) 及 String转JsonArray(Sting转JSONArray)
1)普通net.sf.json.JSONObject
JSONObject json= JSONObject.fromObject(responseMsg);//JSONObject解析
JSONArray listJson = JSONArray.fromObject(responseMsg);//JSONArray解析 "[{},{},{}]"
完整示例:
//6.处理返回的内容
//JSONObject json= JSONObject.fromObject(responseMsg);//JSONObject解析
JSONArray listJson = JSONArray.fromObject(responseMsg);//JSONArray解析 "[{},{},{}]"
for(int i=0; i<listJson.size(); i++){
JSONObject jsonObject = listJson.getJSONObject(i);
String canteenName = jsonObject.getString("canteenName");
String employeeName = jsonObject.getString("employeeName");
System.out.println("canteenName:" + canteenName);
System.out.println("employeeName:" + employeeName);
}
2)阿里云fastjson
//String转Json
JSONObject json = JSONObject.parseObject(token);//JSONObject解析
//String转JSONArray
JSONArray listJson = JSONArray.parseArray(value);//JSONArray解析 "[{},{},{}]"
三、json中带反斜杠\无法解析的问题
普通net.sf.json.JSONObject
import net.sf.json.JSONObject;
String jsonStr = "{\"name\":\"zhangsan\",\"password\":\"123123\"}";
JSONObject json_test = JSONObject.fromObject(jsonStr);
解决json中带反斜杠\无法解析的问题(手工处理)
//解决json中带反斜杠\无法解析的问题(手工处理)
jsonStr = jsonStr.substring(1, jsonStr.length() - 1);
jsonStr = jsonStr.replace("\\", "");
阿里云fastjson
String xmlStr = {"xmlStr":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><APPROVEDATAINFO><SBLSH_SHORT>405051610052012121110300001</SBLSH_SHORT><SXBM>52012120140505161008008244</SXBM><XZQHDM>520100</XZQHDM><EXPRESSTYPE>3</EXPRESSTYPE></APPROVEDATAINFO>"};
JSONObject json = JSONObject.parseObject(xmlStr);
String xml = json.get("xmlStr").toString();
System.out.println(xml);
解决json中带反斜杠\无法解析的问题(阿里云fastjson)
//解决json中带反斜杠\无法解析的问题(阿里云fastjson)
jsonStr = JSON.parse(jsonStr).toString();
参考:问题关于json 字符串中带有反斜杠的问题_ベ远行ミ的博客-CSDN博客_json里面有反斜杠
Java JSON转Map
//JSON转Map(JSONObject是Map接口的一个实现类)
Map json = (Map)JSONObject.parse(str);
参考:Java JSON转换为map_Eric-x的博客-CSDN博客_java json转map
最新示例 (Redis缓存)
Json转实体
// 保存第一页的电子档案信息(Redis缓存中)
// 获取缓存的第一页数据
String key1 = RedisConstant.PREFIX_DOCUMENT + CurrentUserUtil.getCurrentUserInfo().getUserId() + ":" + dto.getSceneId() + ":" + 1;
String jsonStr = redisTemplate.opsForValue().get(key1);
if(!StringUtils.isEmpty(jsonStr)){
// Json转实体
BusinessDto businessDto = JSONObject.parseObject(jsonStr, BusinessDto.class);
if(!CollectionUtils.isEmpty(businessDto.getDocuments())){
}
}
Json转List
// 优化,增加Redis缓存
String key = RedisConstant.PREFIX_ORG_LISTTREE +
(id == null ? "default-" : id + "-") +
(type == null ? "default-" : type + "-") +
(StringUtils.isEmpty(name) ? "default" : name);
String jsonStr = redisTemplate.opsForValue().get(key);
if(!StringUtils.isEmpty(jsonStr)){
// Json转List
List<SysOrganizationVo> listReturn = JSONArray.parseArray(jsonStr, SysOrganizationVo.class);
}
实体转Json
// 实体转Json
JSONObject json = (JSONObject) JSONObject.toJSON(businessDto);
// 保存到Redis
redisTemplate.opsForValue().set(RedisConstant.PREFIX_DOCUMENT + CurrentUserUtil.getCurrentUserInfo().getUserId() + ":" + businessDto.getSceneId() + ":" + businessDto.getWhichPage(), json.toString());
List转Json
// List转Json
JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
// 保存到Redis
redisTemplate.opsForValue().set(key, jsonArray.toString());
更多推荐
已为社区贡献6条内容
所有评论(0)