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

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

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

  1.环境

  jdk1.8,fastjson

  2.pom.xml

[html]  view plain  copy
 
  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.   
  5.     <groupId>co.neutron.json</groupId>  
  6.     <artifactId>fastjson</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>fastjson</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>4.8</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>com.alibaba</groupId>  
  26.             <artifactId>fastjson</artifactId>  
  27.             <version>1.2.12</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.slf4j</groupId>  
  31.             <artifactId>slf4j-log4j12</artifactId>  
  32.             <version>1.7.2</version>  
  33.         </dependency>  
  34.     </dependencies>  
  35. </project>  


  3.实体类

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


  4.测试类

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值