4.使用fastjson进行json字符串和List的转换

时间:2023-03-10 01:33:12
4.使用fastjson进行json字符串和List的转换

转自:https://blog.****.net/lipr86/article/details/80833952

使用fastjson进行自定义类的列表和字符串转换

1.环境

jdk1.8,fastjson

2.pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>co.neutron.json</groupId>
  5. <artifactId>fastjson</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>fastjson</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.8</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>fastjson</artifactId>
  23. <version>1.2.12</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.slf4j</groupId>
  27. <artifactId>slf4j-log4j12</artifactId>
  28. <version>1.7.2</version>
  29. </dependency>
  30. </dependencies>
  31. </project>

3.实体类

  1. package co.neutron.json.fastjson.entity;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private int age;
  6. public User() {
  7. super();
  8. }
  9. public User(int id, String name, int age) {
  10. super();
  11. this.id = id;
  12. this.name = name;
  13. this.age = age;
  14. }
  15. public int getId() {
  16. return id;
  17. }
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public int getAge() {
  28. return age;
  29. }
  30. public void setAge(int age) {
  31. this.age = age;
  32. }
  33. @Override
  34. public String toString() {
  35. return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
  36. }
  37. }

4.测试类

    1. package co.neutron.json.fastjson;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import org.junit.Assert;
    5. import org.junit.Test;
    6. import com.alibaba.fastjson.JSON;
    7. import co.neutron.json.fastjson.entity.User;
    8. public class ArrayListTest {
    9. /*
    10. * 测试内容如下
    11. * 1.将User类型数组转换成json字符串
    12. * 2.将json字符串转换成为User数组
    13. */
    14. @Test
    15. public void testArray2StringAndString2List() {
    16. User user1 = new User(1, "张1", 11);
    17. User user2 = new User(2, "张2", 12);
    18. User user3 = new User(3, "张3", 13);
    19. User user4 = new User(4, "张4", 14);
    20. User[] users = {user1, user2, user3, user4};
    21. /*
    22. * 将数组转换为Json字符串
    23. * result:
    24. * [{"age":11,"id":1,"name":"张1"},{"age":12,"id":2,"name":"张2"},
    25. * {"age":13,"id":3,"name":"张3"},{"age":14,"id":4,"name":"张4"}]
    26. */
    27. String userStr = JSON.toJSONString(users);
    28. /*
    29. * 将Json字符串转换为List
    30. * result
    31. * User [id=1, name=张1, age=11]
    32. User [id=2, name=张2, age=12]
    33. User [id=3, name=张3, age=13]
    34. User [id=4, name=张4, age=14]
    35. */
    36. List<User> userList = JSON.parseArray(userStr, User.class);
    37. userList.stream().forEach(System.err::println);
    38. }
    39. /**
    40. * 测试包装类型的List转换为json字符串
    41. */
    42. @Test
    43. public void testList2String() {
    44. List<Long> longs = new ArrayList<Long>();
    45. longs.add(1L);
    46. longs.add(2L);
    47. longs.add(3L);
    48. String actual = JSON.toJSONString(longs);
    49. Assert.assertEquals("[1,2,3]", actual);
    50. }
    51. }