生活随笔
收集整理的這篇文章主要介紹了
使用fastjson进行json字符串和List的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 使用fastjson進行自定義類的列表和字符串轉換
? 1.環境
? jdk1.8,fastjson
? 2.pom.xml
[html]?view plain
?copy<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.實體類
[html]?view plain
?copypackage?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.測試類
[java]?view plain
?copypackage?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?{??????????????????@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};???????????????????????????????????String?userStr?=?JSON.toJSONString(users);?????????????????????????????????????List<User>?userList?=?JSON.parseArray(userStr,?User.class);??????????userList.stream().forEach(System.err::println);??????}????????????????????@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);??????}????}??
總結
以上是生活随笔為你收集整理的使用fastjson进行json字符串和List的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。