fastjson基本操作

简介: fastjson基本操作

一、引入依赖

   <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.80</version>
        </dependency>

二、对象转JSONString

List转Json

List<Student> students = new ArrayList();
String str = JSON.toJSONString(students); // List转json

三、JSONString装对象

Json 转List 方法一

String json = ""; //获取的Json数据
List<Student> students = JSON.parseObject(json,new TypeReference<List<Student>>(){}); // Json 转List

Json 转List方法二

List<Student> students = JSON.parseArray(json,Student.class);

示例

 List<List<Integer>> res2=new ArrayList<>();
        List<List<Integer>> res=new ArrayList<>();
        List<Integer> list1 = Stream.of(1, 2, 3).collect(Collectors.toList());
        List<Integer> list2 = Stream.of(4,5).collect(Collectors.toList());
        res.add(list1);
        res.add(list2);
        String str = JSON.toJSONString(res);
        System.out.println(str);
        List<List<Integer>> lists = JSON.parseObject(str, new TypeReference<List<List<Integer>>>() {
        });
        System.out.println(lists);

对象需要无参构造器

JSON.parseObject与 new TypeReference<>() { }配合可以构造复杂对象

JSON.parseObject(str, new TypeReference<List<List<Integer>>>() {
        });
相关文章
|
JSON fastjson 数据格式
fastjson中的一些常用方法推荐
fastjson中的一些常用方法推荐
384 0
|
JSON Java 数据格式
Spring Boot返回Json数据及数据封装
在Spring Boot中,接口间及前后端的数据传输通常使用JSON格式。通过@RestController注解,可轻松实现Controller返回JSON数据。该注解是Spring Boot新增的组合注解,结合了@Controller和@ResponseBody的功能,默认将返回值转换为JSON格式。Spring Boot底层默认采用Jackson作为JSON解析框架,并通过spring-boot-starter-json依赖集成了相关库,包括jackson-databind、jackson-datatype-jdk8等常用模块,简化了开发者对依赖的手动管理。
924 3
|
SQL XML Java
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
495 6
|
消息中间件 Java
springboot RabbitMQ 连接超时配置
springboot RabbitMQ 连接超时配置
1265 0
|
存储 JSON Java
JSON.toJSONString什么作用
JSON.toJSONString什么作用
905 0
|
存储 NoSQL Java
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration报错问题处理
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
2378 7
|
Java 应用服务中间件 Maven
【终极解决方案】IDEA maven 项目修改代码不生效。
【终极解决方案】IDEA maven 项目修改代码不生效。
2683 1
|
JSON fastjson Java
使用FastJson
使用FastJson
1255 1
|
存储 JSON Java
Java对象转换为JSON字符串
在Java开发中,常需将数据对象转换为JSON存储,如使用Fastjson库。要将Java对象转为JSON,可调用`JSON.toJSONString(obj)`;反向转换则用`JSON.parseObject(str, Class)`。
760 0