Json-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型

Json-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型

只列举了最省事的方式。不涉及复制情况和速度。

测试用例,一个User类,属性name,age,location。重写toString()。


 
 
  
  
  1. public class User {
  2. private String name;
  3. private Integer age;
  4. private String location;
  5. public User() {
  6. }
  7. public User(String name) {
  8. this.name = name;
  9. }
  10. public User(String name, Integer age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. public User(String name, Integer age, String location) {
  15. this.name = name;
  16. this.age = age;
  17. this.location = location;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public Integer getAge() {
  26. return age;
  27. }
  28. public void setAge(Integer age) {
  29. this.age = age;
  30. }
  31. public String getLocation() {
  32. return location;
  33. }
  34. public void setLocation(String location) {
  35. this.location = location;
  36. }
  37. @Override
  38. public String toString() {
  39. return "User{" +
  40. "name='" + name + '\'' +
  41. ", age=" + age +
  42. ", location='" + location + '\'' +
  43. '}';
  44. }
  45. }

 
 
  
  
  1. 1Json- Lib
  2. maven依赖如下,需注意<classifier>jdk15</classifier>,jar包区分jdk1. 3和jdk1. 5版本
  3. <dependency>
  4. <groupId>net.sf.json-lib</groupId>
  5. <artifactId>json-lib</artifactId>
  6. <version> 2.4</version>
  7. <classifier>jdk15</classifier>
  8. </dependency>
  9. 测试demo
  10. import net.sf.json.JSONObject;
  11. public class JsonLibDemo {
  12. public static void main( String[] args) {
  13. //创建测试object
  14. User user = new User( "李宁", 24, "北京");
  15. System.out. println(user);
  16. //转成json字符串
  17. JSONObject jsonObject = JSONObject.fromObject(user);
  18. String json = jsonObject. toString();
  19. System.out. println(json);
  20. //json字符串转成对象
  21. JSONObject jsonObject1 = JSONObject.fromObject(json);
  22. User user1 = ( User) JSONObject.toBean(jsonObject1, User. class);
  23. System.out. println(user1);
  24. }
  25. }

 
 
  
  
  1. 2、org.json
  2. maven依赖如下
  3. <dependency>
  4. <groupId>org.json</groupId>
  5. <artifactId>json</artifactId>
  6. <version> 20170516</version>
  7. </dependency>
  8. 测试demo
  9. import org.json.JSONObject;
  10. public class OrgJsonDemo {
  11. public static void main(String[] args) {
  12. //创建测试object
  13. User user = new User( "李宁", 24, "北京");
  14. System. out.println(user);
  15. //转成json字符串
  16. String json = new JSONObject(user).toString();
  17. System. out.println(json);
  18. //json字符串转成对象
  19. JSONObject jsonObject = new JSONObject(json);
  20. String name = jsonObject.getString( "name");
  21. Integer age = jsonObject.getInt( "age");
  22. String location = jsonObject.getString( "location");
  23. User user1 = new User(name,age,location);
  24. System. out.println(user1);
  25. }
  26. }

 
 
  
  
  1. 3、Jackson
  2. maven依赖
  3. <dependency>
  4. <groupId>com.fasterxml.jackson.core</groupId>
  5. <artifactId>jackson-databind</artifactId>
  6. <version> 2.9 .0</version>
  7. </dependency>
  8. 测试demo
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. public class JacksonDemo {
  11. public static void main(String[] args) {
  12. //创建测试object
  13. User user = new User( "李宁", 24, "北京");
  14. System. out.println(user);
  15. //转成json字符串
  16. ObjectMapper mapper = new ObjectMapper();
  17. try {
  18. String json = mapper.writeValueAsString(user);
  19. System. out.println(json);
  20. //json字符串转成对象
  21. User user1 = mapper.readValue(json,User.class);
  22. System. out.println(user1);
  23. } catch (java.io.IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }

 
 
  
  
  1. 4、Gson
  2. maven依赖
  3. <dependency>
  4. <groupId>com.google.code.gson</groupId>
  5. <artifactId>gson</artifactId>
  6. <version> 2.8 .1</version>
  7. </dependency>
  8. 测试demo
  9. import com.google.gson.Gson;
  10. public class GsonDemo {
  11. public static void main(String[] args) {
  12. //创建测试object
  13. User user = new User( "李宁", 24, "北京");
  14. System. out.println(user);
  15. //转成json字符串
  16. Gson gson = new Gson();
  17. String json = gson.toJson(user);
  18. System. out.println(json);
  19. //json字符串转成对象
  20. User user1 = gson.fromJson(json,User.class);
  21. System. out.println(user1);
  22. }
  23. }

 
 
  
  
  1. 5FastJson
  2. maven依赖
  3. <dependency>
  4. <groupId>com.alibaba</groupId>
  5. <artifactId>fastjson</artifactId>
  6. <version> 1.2. 37</version>
  7. </dependency>
  8. 测试demo
  9. import com.alibaba.fastjson.JSON;
  10. public class FastJsonDemo {
  11. public static void main( String[] args) {
  12. //创建测试object
  13. User user = new User( "李宁", 24, "北京");
  14. System.out. println(user);
  15. //转成json字符串
  16. String json = JSON.toJSON(user). toString();
  17. System.out. println(json);
  18. //json字符串转成对象
  19. User user1 = JSON.parseObject(json, User. class);
  20. System.out. println(user1);
  21. }
  22. }

json-lib时间有些久远,jar包只更新到2010年

org.json用起来有些繁琐

Jackson、Gson、FastJson只需一两句话就可以搞定

原文出自:https://blog.csdn.net/n447194252/article/details/77747465

扫描二维码关注公众号,回复: 15192203 查看本文章

Json-Lib、Org.Json、Jackson、Gson、FastJson五种方式转换json类型

猜你喜欢

转载自blog.csdn.net/cuiyuchen111/article/details/90644070