当前位置:   article > 正文

Java 实体转Json Json转实体 String转Json Sting转JSONObject Sting转JSONArray_java 实体类转json

java 实体类转json

常用:String转Json 字符串转Json

  1. JSONObject json = JSONObject.parseObject(token);
  2. token = json.get("token").toString();

一、实体转Json Json转实体 String转Json String转JSONArray

  1. 一、JDKjson(json-lib-2.4-jdk15.jar)
  2. // String转Json
  3. JSONObject json= JSONObject.fromObject(responseMsg);//JSONObject解析
  4. // String转JSONArray
  5. JSONArray listJson = JSONArray.fromObject(responseMsg);//JSONArray解析 "[{},{},{}]"
  6. // 实体转Json
  7. JSONObject json = JSONObject.fromObject(std);
  8. // List转Json
  9. JSONArray jsonArray = JSONArray.fromObject(list);
  10. // Json转实体
  11. StdCost stdTemp = (StdCost)JSONObject.toBean(json, StdCost.class);
  12. // Json转List
  13. List<StdCost> listTemp = JSONArray.toList(jsonArray, StdCost.class);
  14. 二、阿里云fastjson(fastjson-1.2.10.jar)
  15. // String转Json
  16. JSONObject json = JSONObject.parseObject(responseMsg);//JSONObject解析
  17. // String转JSONArray
  18. JSONArray listJson = JSONArray.parseArray(responseMsg);//JSONArray解析 "[{},{},{}]"
  19. // 实体转Json
  20. JSONObject json = (JSONObject)JSONObject.toJSON(std);
  21. // List转Json
  22. JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
  23. // Json转实体
  24. StdCost stdJson = JSONObject.parseObject(json.toString(), StdCost.class);
  25. // Json转List
  26. List<StdCost> listTemp = JSONArray.parseArray(jsonArray.toString(), StdCost.class);

完整示例:

  1. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
  2. if(list != null && list.size() > 0){
  3. /*for(int i=0; i<list.size(); i++){
  4. StdCost std = list.get(i);
  5. System.out.println("--- " + i + "/" + list.size() +
  6. " " + std.getStudentName() +
  7. " " + std.getStudentIdCard() +
  8. " " + std.getFeitype() +
  9. " " + std.getFei() +
  10. " " + df.format(std.getCreatedDate())
  11. );
  12. JSONObject json = JSONObject.fromObject(std);
  13. listJson.add(json);
  14. StdCost stdTemp = (StdCost)JSONObject.toBean(json, StdCost.class);
  15. System.out.println(stdTemp.getStudentName() +
  16. " " + stdTemp.getStudentIdCard() +
  17. " " + stdTemp.getFeitype() +
  18. " " + stdTemp.getFei() +
  19. " " + df.format(stdTemp.getCreatedDate())
  20. );*/
  21. /*JSONObject json = (JSONObject)JSONObject.toJSON(std);
  22. listJson.add(json);
  23. StdCost stdTemp = JSONObject.parseObject(json.toString(), StdCost.class);
  24. System.out.println(stdTemp.getStudentName() +
  25. " " + stdTemp.getStudentIdCard() +
  26. " " + stdTemp.getFeitype() +
  27. " " + stdTemp.getFei() +
  28. " " + df.format(stdTemp.getCreatedDate())
  29. );*/
  30. //}
  31. //jsonArray = JSONArray.fromObject(list);
  32. //List<StdCost> listTemp = JSONArray.toList(jsonArray, StdCost.class);
  33. jsonArray = (JSONArray)JSONArray.toJSON(list);
  34. List<StdCost> listTemp = JSONArray.parseArray(jsonArray.toString(), StdCost.class);
  35. if(listTemp != null && listTemp.size() > 0){
  36. StdCost stdTemp = null;
  37. for(int i=0; i<listTemp.size(); i++){
  38. stdTemp = listTemp.get(i);
  39. System.out.println(stdTemp.getStudentName() +
  40. " " + stdTemp.getStudentIdCard() +
  41. " " + stdTemp.getFeitype() +
  42. " " + stdTemp.getFei() +
  43. " " + df.format(stdTemp.getCreatedDate())
  44. );
  45. }
  46. }
  47. }

二、String转Json(Sting转JSONObject) 及 String转JsonArray(Sting转JSONArray)

1)普通net.sf.json.JSONObject

  1. JSONObject json= JSONObject.fromObject(responseMsg);//JSONObject解析
  2. JSONArray listJson = JSONArray.fromObject(responseMsg);//JSONArray解析 "[{},{},{}]"

完整示例:

  1. //6.处理返回的内容
  2. //JSONObject json= JSONObject.fromObject(responseMsg);//JSONObject解析
  3. JSONArray listJson = JSONArray.fromObject(responseMsg);//JSONArray解析 "[{},{},{}]"
  4. for(int i=0; i<listJson.size(); i++){
  5. JSONObject jsonObject = listJson.getJSONObject(i);
  6. String canteenName = jsonObject.getString("canteenName");
  7. String employeeName = jsonObject.getString("employeeName");
  8. System.out.println("canteenName:" + canteenName);
  9. System.out.println("employeeName:" + employeeName);
  10. }

2)阿里云fastjson

  1. //String转Json
  2. JSONObject json = JSONObject.parseObject(token);//JSONObject解析
  3. //String转JSONArray
  4. JSONArray listJson = JSONArray.parseArray(value);//JSONArray解析 "[{},{},{}]"

  

三、json中带反斜杠\无法解析的问题

普通net.sf.json.JSONObject

  1. import net.sf.json.JSONObject;
  2. String jsonStr = "{\"name\":\"zhangsan\",\"password\":\"123123\"}";
  3. JSONObject json_test = JSONObject.fromObject(jsonStr);

解决json中带反斜杠\无法解析的问题(手工处理) 

  1. //解决json中带反斜杠\无法解析的问题(手工处理)
  2. jsonStr = jsonStr.substring(1, jsonStr.length() - 1);
  3. jsonStr = jsonStr.replace("\\", "");
阿里云fastjson
  1. 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>"};
  2. JSONObject json = JSONObject.parseObject(xmlStr);
  3. String xml = json.get("xmlStr").toString();
  4. System.out.println(xml);

解决json中带反斜杠\无法解析的问题(阿里云fastjson) 

  1. //解决json中带反斜杠\无法解析的问题(阿里云fastjson)
  2. jsonStr = JSON.parse(jsonStr).toString();

 参考:问题关于json 字符串中带有反斜杠的问题_ベ远行ミ的博客-CSDN博客_json里面有反斜杠

Java JSON转Map 

  1. //JSON转Map(JSONObject是Map接口的一个实现类)
  2. Map json = (Map)JSONObject.parse(str);

 参考:Java JSON转换为map_Eric-x的博客-CSDN博客_java json转map

最新示例 (Redis缓存)

Json转实体

  1. // 保存第一页的电子档案信息(Redis缓存中)
  2. // 获取缓存的第一页数据
  3. String key1 = RedisConstant.PREFIX_DOCUMENT + CurrentUserUtil.getCurrentUserInfo().getUserId() + ":" + dto.getSceneId() + ":" + 1;
  4. String jsonStr = redisTemplate.opsForValue().get(key1);
  5. if(!StringUtils.isEmpty(jsonStr)){
  6. // Json转实体
  7. BusinessDto businessDto = JSONObject.parseObject(jsonStr, BusinessDto.class);
  8. if(!CollectionUtils.isEmpty(businessDto.getDocuments())){
  9. }
  10. }

 Json转List

  1. // 优化,增加Redis缓存
  2. String key = RedisConstant.PREFIX_ORG_LISTTREE +
  3. (id == null ? "default-" : id + "-") +
  4. (type == null ? "default-" : type + "-") +
  5. (StringUtils.isEmpty(name) ? "default" : name);
  6. String jsonStr = redisTemplate.opsForValue().get(key);
  7. if(!StringUtils.isEmpty(jsonStr)){
  8. // Json转List
  9. List<SysOrganizationVo> listReturn = JSONArray.parseArray(jsonStr, SysOrganizationVo.class);
  10. }

实体转Json 

  1. // 实体转Json
  2. JSONObject json = (JSONObject) JSONObject.toJSON(businessDto);
  3. // 保存到Redis
  4. redisTemplate.opsForValue().set(RedisConstant.PREFIX_DOCUMENT + CurrentUserUtil.getCurrentUserInfo().getUserId() + ":" + businessDto.getSceneId() + ":" + businessDto.getWhichPage(), json.toString());

List转Json

  1. // List转Json
  2. JSONArray jsonArray = (JSONArray)JSONArray.toJSON(list);
  3. // 保存到Redis
  4. redisTemplate.opsForValue().set(key, jsonArray.toString());

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/1014146
推荐阅读
相关标签
  

闽ICP备14008679号