Json-Lib, Org.Json, Jackson, Gson, FastJson five ways to convert json type

Json-Lib, Org.Json, Jackson, Gson, FastJson five ways to convert json type

Only the most convenient way is listed. Replication situation and speed are not involved.

Test case, a User class, attribute name, age, location. Override 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. 1 Json- Lib
  2. Maven depends on the following, pay attention to <classifier>jdk15</classifier>, jar package distinguishes jdk1. 3 and jdk1. 5 version
  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. test demo
  10. import net.sf.json.JSONObject;
  11. public class JsonLibDemo {
  12. public static void main( String[] args) {
  13. //Create test object
  14. User user = new User( "Li Ning" , 24, "Beijing" );
  15. System.out. println(user);
  16. //Convert to json string
  17. JSONObject jsonObject = JSONObject.fromObject(user);
  18. String json = jsonObject. toString();
  19. System .out. println(json);
  20. //json string to object
  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. The maven dependencies are as follows
  3. <dependency>
  4. <groupId>org.json</groupId>
  5. <artifactId>json</artifactId>
  6. <version> 20170516</version>
  7. </dependency>
  8. test demo
  9. import org.json.JSONObject;
  10. public class OrgJsonDemo {
  11. public static void main(String[] args) {
  12. //Create test object
  13. User user = new User( "Li Ning" , 24, "Beijing" );
  14. System. out.println(user);
  15. //Convert to json string
  16. String json = new JSONObject(user).toString();
  17. System. out.println(json);
  18. //json string to object
  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 dependencies
  3. <dependency>
  4. <groupId>com.fasterxml.jackson.core</groupId>
  5. <artifactId>jackson-databind</artifactId>
  6. <version> 2.9 .0</version>
  7. </dependency>
  8. test demo
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. public class JacksonDemo {
  11. public static void main(String[] args) {
  12. //Create test object
  13. User user = new User( "Li Ning" , 24, "Beijing" );
  14. System. out.println(user);
  15. //Convert to json string
  16. ObjectMapper mapper = new ObjectMapper();
  17. try {
  18. String json = mapper.writeValueAsString(user);
  19. System. out.println(json);
  20. //json string to object
  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 dependencies
  3. <dependency>
  4. <groupId>com.google.code.gson</groupId>
  5. <artifactId>gson</artifactId>
  6. <version> 2.8 .1</version>
  7. </dependency>
  8. test demo
  9. import com.google.gson.Gson;
  10. public class GsonDemo {
  11. public static void main(String[] args) {
  12. //Create test object
  13. User user = new User( "Li Ning" , 24, "Beijing" );
  14. System. out.println(user);
  15. //Convert to json string
  16. Gson gson = new Gson();
  17. String json = gson.toJson(user);
  18. System. out.println(json);
  19. //json string to object
  20. User user1 = gson.fromJson(json,User.class);
  21. System. out.println(user1);
  22. }
  23. }

 


 
 
  
  
  1. 5 FastJson
  2. maven dependencies
  3. <dependency>
  4. <groupId>com.alibaba</groupId>
  5. <artifactId>fastjson</artifactId>
  6. <version> 1.2. 37</version>
  7. </dependency>
  8. test demo
  9. import com.alibaba.fastjson.JSON;
  10. public class FastJsonDemo {
  11. public static void main( String[] args) {
  12. //Create test object
  13. User user = new User( "Li Ning" , 24, "Beijing" );
  14. System.out. println(user);
  15. //Convert to json string
  16. String json = JSON.toJSON(user). toString();
  17. System .out. println(json);
  18. //json string to object
  19. User user1 = JSON.parseObject(json, User. class);
  20. System .out. println(user1);
  21. }
  22. }

 

The json-lib time is a bit long, and the jar package is only updated to 2010

org.json is a bit cumbersome to use

Jackson, Gson, FastJson can be done in just one or two sentences

Original text from: https://blog.csdn.net/n447194252/article/details/77747465

Json-Lib, Org.Json, Jackson, Gson, FastJson five ways to convert json type

Guess you like

Origin blog.csdn.net/cuiyuchen111/article/details/90644070