JsonTools.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cn.com.lzt.tools;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import net.sf.json.JSONArray;
  5. import net.sf.json.JSONObject;
  6. import org.codehaus.jackson.map.ObjectMapper;
  7. import org.jeecgframework.web.system.pojo.base.TSUser;
  8. import org.json.JSONException;
  9. public class JsonTools {
  10. /**
  11. * 将json转化为实体POJO
  12. * @param jsonStr
  13. * @param obj
  14. * @return
  15. */
  16. public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
  17. T t = null;
  18. try {
  19. ObjectMapper objectMapper = new ObjectMapper();
  20. t = objectMapper.readValue(jsonStr,
  21. obj);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. return t;
  26. }
  27. /**
  28. * 将实体POJO转化为JSON
  29. * @param obj
  30. * @return
  31. * @throws JSONException
  32. * @throws IOException
  33. */
  34. // public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
  35. // ObjectMapper mapper = new ObjectMapper();
  36. // // Convert object to JSON string
  37. // String jsonStr = "";
  38. // try {
  39. // jsonStr = mapper.writeValueAsString(obj);
  40. // } catch (IOException e) {
  41. // throw e;
  42. // }
  43. // return new JSONObject(jsonStr);
  44. // }
  45. public static void main(String[] args) throws JSONException, IOException {
  46. JSONObject obj = null;
  47. obj = new JSONObject();
  48. // obj.put("userName", "213");
  49. // obj.put("id", 27);
  50. // JSONArray array = new JSONArray();
  51. // array.put(obj);
  52. // obj = new JSONObject();
  53. // obj.put("userName", "214");
  54. // obj.put("id", 28);
  55. // array.put(obj);
  56. String ostr = "{\"errcode\":0,\"errmsg\":\"ok\",\"userlist\":[{\"userName\":\"卓感科技\",\"id\":\"manager6528\"},{\"userName\":\"程尧\",\"id\":\"03460032393259\"},{\"userName\":\"陈旭东\",\"id\":\"03424335654855\"},{\"userName\":\"董广群\",\"id\":\"03443148156837\"}]}";
  57. obj = JSONObject.fromObject(ostr);
  58. System.out.println( obj.get("userlist"));
  59. JSONArray json = JSONArray.fromObject(obj.get("userlist"));
  60. List<TSUser> persons = (List<TSUser>)JSONArray.toList(json, TSUser.class);
  61. System.out.println(persons);
  62. // Object user = JSONToObj("\"userList\":" + obj.get("userlist").toString(), UserList.class);
  63. // System.out.println( user);
  64. // JSONObject jsStr = JSONObject.fromObject("{"+obj.get("userlist").toString()+"}");
  65. // Student stu = (Student) JSONToObj(obj.toString(), Student.class);
  66. // JSONObject objList = new JSONObject();
  67. // objList.put("student", array);
  68. // System.out.println("objList:"+objList);
  69. // StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
  70. // System.out.println("student:"+stu);
  71. // System.out.println("stuList:"+stuList);
  72. // System.out.println("#####################################");
  73. // JSONObject getObj = objectToJson(stu);
  74. // System.out.println(getObj);
  75. }
  76. }