1. public class JsonTest {
    2. //准备存放了一个对象的json字符串
    3. private String userStr = "{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"}";
    4. //准备存放了一个集合的json字符串
    5. private String userArr = "[{\"id\":1,\"name\":\"简溪\",\"birthday\":\"2010-01-01\"},"+ "{\"id\":2,\"name\":\"顾里\",\"birthday\":\"2012-08-01\"}]";
    6. private User user;
    7. private List<User> users = new ArrayList<User>();
    8. @Before
    9. public void initUser() {
    10. //初始化一个用户
    11. User user = new User();
    12. user.setId(1);
    13. user.setName("林萧");
    14. user.setBirthday(new Date());
    15. this.user = user;
    16. //初始化一个集合
    17. User user1 = new User();
    18. user1.setId(1);
    19. user1.setName("林萧");
    20. user1.setBirthday(new Date());
    21. users.add(user1);
    22. User user2 = new User();
    23. user2.setId(2);
    24. user2.setName("顾源");
    25. user2.setBirthday(new Date());
    26. users.add(user2);
    27. }
    28. //fastjson
    29. @Test
    30. public void fastjson_json2Bean() {
    31. //[1-1] 对象格式json串转对象
    32. User user = JSONObject.parseObject(userStr, User.class);
    33. System.out.println(user);
    34. //打印效果如下:
    35. //User [id=1, name=简溪, birthday=2010-01-01]
    36. //[1-2] 集合格式json串转对象
    37. List<User> users = JSONArray.parseArray(userArr, User.class);
    38. System.out.println(users);
    39. //打印效果如下:
    40. // [User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
    41. }
    42. @Test
    43. public void fastjson_bean2Json() {
    44. //[1-3] 对象转json串
    45. String userJson = JSONObject.toJSONString(user);
    46. System.out.println(userJson);
    47. //打印效果如下:
    48. // {"birthday":1530716739741,"id":1,"name":"林萧"}
    49. //[1-4] 集合转json串
    50. String usersStr = JSONArray.toJSONString(users);
    51. System.out.println(usersStr);
    52. //打印效果如下:
    53. // [{"birthday":1530716739741,"id":1,"name":"林萧"},{"birthday":1530716739741,"id":2,"name":"顾源"}]
    54. }
    55. //jackson
    56. @Test
    57. public void jackson_json2Bean() throws Exception {
    58. //[2-1] 对象格式json串转对象
    59. ObjectMapper jackson = new ObjectMapper();
    60. User user = jackson.readValue(userStr, User.class);
    61. System.out.println(user);
    62. //打印结果
    63. //User [id=1, name=简溪, birthday=2010-01-01]
    64. //[2-2 对象格式json串转对象
    65. List<User> users = jackson.readValue(userArr, new TypeReference<List<User>>() {});
    66. System.out.println(users);
    67. //打印结果
    68. //[User [id=1, name=简溪, birthday=2010-01-01], User [id=2, name=顾里, birthday=2012-08-01]]
    69. }
    70. @Test
    71. public void jackson_bean2Json() throws Exception {
    72. ObjectMapper jackson = new ObjectMapper();
    73. //[2-3] 对象转json串
    74. String userJson = jackson.writeValueAsString(user);
    75. System.out.println(userJson);
    76. //打印结果
    77. // {"id":1,"name":"林萧","birthday":1530717373507}
    78. //[2-4] 集合转json串
    79. String usersJson = jackson.writeValueAsString(users);
    80. System.out.println(usersJson);
    81. //打印结果
    82. // [{"id":1,"name":"林萧","birthday":1530717401460},{"id":2,"name":"顾源","birthday":1530717401460}]
    83. }
    84. }