转自: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. 【转】CentOS下firefox安装flash说明

    http://www.cnblogs.com/lamper/archive/2013/01/16/2862254.htm CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装 ...

  2. arcgis engine 获取高亮Feature、element

    转自原文 arcgis engine 获取高亮Feature.element IGraphicsContainer pGraphicsC =  mainAxMapControl.Map as IGra ...

  3. SDSoC使用体验

    本文作者:卜居 转载请保留作者信息.原文网址(http://blog.csdn.net/kkk584520/article/details/47220575). 本文project可到我的资源下载(h ...

  4. bzoj 1010 (单调决策优化)

    能够非常好的证明单调决策性质.用   记sum[i]=sigma(C[1],C[2].....C[k]);f[i]=sum[i]+i;  c=l-1; 有转移dp[i]=min( dp[j]+(f[i ...

  5. Cookie与Session的区别与联系及生命周期

    Cookie与Session的区别与联系及生命周期 一.Session与Cookie介绍 这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会 ...

  6. 【Henu ACM Round#14 B】Duff in Love

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让你在n的因子里面找一个最大的数字x 且x的因子全都不是完全平方数(y^2,y>1) O(sqrt(n))找出n的所有因子. ...

  7. 高速排序——JAVA实现(图文并茂)

    高快省的排序算法 有没有既不浪费空间又能够快一点的排序算法呢?那就是"高速排序"啦! 光听这个名字是不是就认为非常高端呢. 如果我们如今对"6 1 2 7 9 3 4 5 ...

  8. Oracle HR 例子用户的建立 10g,11g均可

    Oracle HR 例子用户的建立 10g,11g均可 先将附件(见文章尾部)上的 10 个 .sql 文件放入这个路径中 : $ORACLE_HOME/demo/schema/human_resou ...

  9. vim-normal多行操作命令的使用

    命令行命令-<:normal>这个命令可以重复上一个操作.他其实就跟.命令的效果查不到.不同的是,他可以把.的效果,作用于你用可视模式下的多行.例如,如果你想在下面的文字里在每一行加一个; ...

  10. js插件---JS表格组件BootstrapTable行内编辑解决方案x-editable

    js插件---JS表格组件BootstrapTable行内编辑解决方案x-editable 一.总结 一句话总结:bootstrap能够做为最火的框架,绝对不仅仅只有我看到的位置,它应该还有很多位置可 ...