日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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?copy
  • 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.測試類

    [java]?view plain?copy
  • 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);??
  • ????}??
  • ??
  • }??
  • 總結

    以上是生活随笔為你收集整理的使用fastjson进行json字符串和List的转换的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。