? 使用fastjson進行自定義類的列表和字符串轉換
? 1.環境
? jdk1.8,fastjson
? 2.pom.xml
- <project?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">??
- ????<modelVersion>4.0.0</modelVersion>??
- ??
- ????<groupId>co.neutron.json</groupId>??
- ????<artifactId>fastjson</artifactId>??
- ????<version>0.0.1-SNAPSHOT</version>??
- ????<packaging>jar</packaging>??
- ??
- ????<name>fastjson</name>??
- ????<url>http://maven.apache.org</url>??
- ??
- ????<properties>??
- ????????<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>??
- ????</properties>??
- ??
- ????<dependencies>??
- ????????<dependency>??
- ????????????<groupId>junit</groupId>??
- ????????????<artifactId>junit</artifactId>??
- ????????????<version>4.8</version>??
- ????????????<scope>test</scope>??
- ????????</dependency>??
- ????????<dependency>??
- ????????????<groupId>com.alibaba</groupId>??
- ????????????<artifactId>fastjson</artifactId>??
- ????????????<version>1.2.12</version>??
- ????????</dependency>??
- ????????<dependency>??
- ????????????<groupId>org.slf4j</groupId>??
- ????????????<artifactId>slf4j-log4j12</artifactId>??
- ????????????<version>1.7.2</version>??
- ????????</dependency>??
- ????</dependencies>??
- </project>??
? 3.實體類
- package?co.neutron.json.fastjson.entity;??
- ??
- public?class?User?{??
- ????private?int?id;??
- ????private?String?name;??
- ????private?int?age;??
- ??????
- ????public?User()?{??
- ????????super();??
- ????}??
- ??
- ????public?User(int?id,?String?name,?int?age)?{??
- ????????super();??
- ????????this.id?=?id;??
- ????????this.name?=?name;??
- ????????this.age?=?age;??
- ????}??
- ??
- ????public?int?getId()?{??
- ????????return?id;??
- ????}??
- ??
- ????public?void?setId(int?id)?{??
- ????????this.id?=?id;??
- ????}??
- ??
- ????public?String?getName()?{??
- ????????return?name;??
- ????}??
- ??
- ????public?void?setName(String?name)?{??
- ????????this.name?=?name;??
- ????}??
- ??
- ????public?int?getAge()?{??
- ????????return?age;??
- ????}??
- ??
- ????public?void?setAge(int?age)?{??
- ????????this.age?=?age;??
- ????}??
- ??
- ????@Override??
- ????public?String?toString()?{??
- ????????return?"User?[id="?+?id?+?",?name="?+?name?+?",?age="?+?age?+?"]";??
- ????}??
- ??????
- }??
? 4.測試類
- package?co.neutron.json.fastjson;??
- ??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- import?org.junit.Assert;??
- import?org.junit.Test;??
- ??
- import?com.alibaba.fastjson.JSON;??
- ??
- import?co.neutron.json.fastjson.entity.User;??
- ??
- public?class?ArrayListTest?{??
- ??
- ????/*?
- ?????*?測試內容如下?
- ?????*?1.將User類型數組轉換成json字符串?
- ?????*?2.將json字符串轉換成為User數組?
- ?????*/??
- ????@Test??
- ????public?void?testArray2StringAndString2List()?{??
- ????????User?user1?=?new?User(1,?"張1",?11);??
- ????????User?user2?=?new?User(2,?"張2",?12);??
- ????????User?user3?=?new?User(3,?"張3",?13);??
- ????????User?user4?=?new?User(4,?"張4",?14);??
- ????????User[]?users?=?{user1,?user2,?user3,?user4};??
- ??????????
- ????????/*??
- ?????????*?將數組轉換為Json字符串?
- ?????????*?result:?
- ?????????*?[{"age":11,"id":1,"name":"張1"},{"age":12,"id":2,"name":"張2"},?
- ?????????*?{"age":13,"id":3,"name":"張3"},{"age":14,"id":4,"name":"張4"}]?
- ?????????*/??
- ????????String?userStr?=?JSON.toJSONString(users);??
- ??????????
- ????????/*?
- ?????????*?將Json字符串轉換為List?
- ?????????*?result?
- ?????????*?User?[id=1,?name=張1,?age=11]?
- ???????????User?[id=2,?name=張2,?age=12]?
- ???????????User?[id=3,?name=張3,?age=13]?
- ???????????User?[id=4,?name=張4,?age=14]?
- ?????????*/??
- ????????List<User>?userList?=?JSON.parseArray(userStr,?User.class);??
- ????????userList.stream().forEach(System.err::println);??
- ????}??
- ??????
- ????/**?
- ?????*?測試包裝類型的List轉換為json字符串?
- ?????*/??
- ????@Test??
- ????public?void?testList2String()?{??
- ????????List<Long>?longs?=?new?ArrayList<Long>();??
- ????????longs.add(1L);??
- ????????longs.add(2L);??
- ????????longs.add(3L);??
- ????????String?actual?=?JSON.toJSONString(longs);??
- ????????Assert.assertEquals("[1,2,3]",?actual);??
- ????}??
- ??
- }??