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

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

1.环境

jdk1.8,fastjson

2.pom.xml

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

3.实体类

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

4.测试类

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

4.使用fastjson进行json字符串和List的转换的更多相关文章

  1. fastjson生成JSON字符串的时候出现$ref

    fastjson生成JSON字符串的时候出现$ref 转载自:http://wuzhuti.cn/201426!826!05!130202.html 可以通过选项 DisableCircularRef ...

  2. Scala中使用fastJson 解析json字符串

    Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...

  3. Java基础97 json插件的使用(java对象和json字符串对象之间的转换)

    1.需要用到的包 2.实例 实体类 people package com.shore.entity; /** * @author DSHORE/2019-4-19 * */ public class ...

  4. fastjson 返回json字符串,JSON.parse 报错

    这是由于转义字符引起的如 : \ , fastjson 处理后是双反斜杠:\\ ,而 JSON.parse 解析时需要4个反斜杠 ,即 js解析json 反斜杠时,需要 4个 解成 1 个 解决方法: ...

  5. fastjson将json字符串转化成map的五种方法

    package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObj ...

  6. 使用Fastjson生成Json字符串少字段属性(数据丢失)

    最后是控制台打印生成的结果如下:FastJson生成字符串是:{"id":"2","name":"节点1"," ...

  7. fastjson对json字符串JSONObject和JSONArray互相转换操作示例

    2017-03-25 直接上代码: package com.tapt.instance; import com.alibaba.fastjson.JSON; import com.alibaba.fa ...

  8. fastjson将json字符串中时间戳转化为日期

    开发中,调用接口,往往会返回一个json字符串.对于json中的时间戳应该如何转为日期对象呢? 定义一个DateValueFilter类,这个类实现了fastjson中ValueFilter接口.其作 ...

  9. fastjson 将json字符串转化成List<Map<String, Object>>

    亲测可行,如下: JSON.parseObject(jsonstr, new TypeReference<List<Map<String, Object>>>() ...

随机推荐

  1. [转] C# HttpWebRequest 绝技

    c# HttpWebRequest与HttpWebResponse绝技    阅读原文 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的G ...

  2. PatentTips - Managing sequenced lock requests

    BACKGROUND In a multi-threaded processing environment, two or more threads may require access to a c ...

  3. 使用ThoughtWorks.QRCode生成二维码

    新建Windows应用程序,加入引用ThoughtWorks.QRCode.dll,编写代码生成二维码. using System; using System.Drawing; using Syste ...

  4. 仿小米简约Calculator

    上个星期的时候,我想教我朋友做一个简单的app.想来想去教什么比較好.当时看见小米的计算器认为比較美丽,就想这个简单.然后就開始动手做了.我以为能够一个小时能够搞定.没想到花了快一天的时间. 哎.突然 ...

  5. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  6. rgba

    正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  7. Task Scheduler

    https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx#BKMK_winui If Task Scheduler is n ...

  8. 29.AngularJS 简介

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 H ...

  9. spring-security-oauth2注解详解

    spring-security-oauth2支持的注解有: 1.EnableOAuth2Client 适用于使用spring security,并且想从Oauth2认证服务器来获取授权的web应用环境 ...

  10. 直接修改Android软件数据库来改变软件设置实例一则

    昨天把K860i刷了机,刷到了最新的CyanogenMod 10.1,使用起来各种流畅舒服,不过却由于内外置SD卡的挂载点的改变,造成了一些困扰,现记录如下.         平时里由于极少把手机连接 ...