zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    hdu4665 DFS
    hdu4665 DFS
    hdu4717 三分(散点的移动)
    POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
    洛谷 P2347 砝码称重
    洛谷 P3009 [USACO11JAN]利润Profits
    洛谷 P2925 [USACO08DEC]干草出售Hay For Sale
    洛谷 P1616 疯狂的采药
    洛谷 P1086 花生采摘
    洛谷 P1048 采药
  • 原文地址:https://www.cnblogs.com/sharpest/p/7879836.html
Copyright © 2011-2022 走看看