使用jackson和fastjson实现list与json互转

简介: 使用jackson和fastjson实现list与json互转

一、依赖

fastjson依赖

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

jackson依赖

springMVC默认json解析器就是jackson,如果是springweb项目不用导入

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.7.3</version>
</dependency>

二、代码

package xxxx.entity;
 
 
 
 
 
 
public class DeliveryMan implements Serializable {
 
    private String name;
 
    private String phone;
    private String area;
    private String officeholding;
    private String jobnum;
    private int state;
 
    public DeliveryMan() {
    }
 
    public DeliveryMan(String name, String phone, String area, String officeholding, String jobnum, int state) {
        this.name = name;
        this.phone = phone;
        this.area = area;
        this.officeholding = officeholding;
        this.jobnum = jobnum;
        this.state = state;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getArea() {
        return area;
    }
 
    public void setArea(String area) {
        this.area = area;
    }
 
    public String getOfficeholding() {
        return officeholding;
    }
 
    public void setOfficeholding(String officeholding) {
        this.officeholding = officeholding;
    }
 
    public String getJobnum() {
        return jobnum;
    }
 
    public void setJobnum(String jobnum) {
        this.jobnum = jobnum;
    }
 
    public int getState() {
        return state;
    }
 
    public void setState(int state) {
        this.state = state;
    }
 
    @Override
    public String toString() {
        return "DeliveryMan{" +
                "name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", area='" + area + '\'' +
                ", officeholding='" + officeholding + '\'' +
                ", jobnum='" + jobnum + '\'' +
                ", state=" + state +
                '}';
    }
    
}
package xxxx.xxx;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uublue.food.Application;
import com.uublue.food.delivery.entity.DeliveryMan;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.IOException;
import java.util.ArrayList;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UtilPay {   
    @Test
    public void jsontostring() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
 
        DeliveryMan man=new DeliveryMan();
        man.setState(1);
        man.setPhone("1111");
 
        DeliveryMan man2=new DeliveryMan();
        man2.setState(2);
        man2.setPhone("2222");
 
        ArrayList<DeliveryMan> list=new ArrayList<>();
        list.add(man);
        list.add(man2);
//list转string
        String s1 = JSONArray.toJSONString(list);
        String s2 = mapper.writeValueAsString(list);
        System.out.println("com.alibaba.fastjson.JSONArray;");
        System.out.println(s1);
        System.out.println("om.fasterxml.jackson.databind.ObjectMapper;");
        System.out.println(s2);
//string转list
        java.util.List<DeliveryMan>  deliveryManList1= JSON.parseArray(s1, DeliveryMan.class);
        java.util.List<DeliveryMan>  deliveryManList2= mapper.readValue(s2, mapper.getTypeFactory().constructParametricType(ArrayList.class, DeliveryMan.class));
 
        System.out.println("com.alibaba.fastjson.JSONArray");
        System.out.println(deliveryManList1.toString());
        System.out.println("om.fasterxml.jackson.databind.ObjectMapper");
        System.out.println(deliveryManList2.toString());
 
    }
}
com.alibaba.fastjson.JSONArray;
[{"phone":"1111","state":1,"user_id":0},{"phone":"2222","state":2,"user_id":0}]
om.fasterxml.jackson.databind.ObjectMapper;
[{"id":null,"name":null,"phone":"1111","area":null,"officeholding":null,"jobnum":null,"state":1,"createtime":null,"updatetime":null,"user_id":0},{"id":null,"name":null,"phone":"2222","area":null,"officeholding":null,"jobnum":null,"state":2,"createtime":null,"updatetime":null,"user_id":0}]
com.alibaba.fastjson.JSONArray
[DeliveryMan{name='null', phone='1111', area='null', officeholding='null', jobnum='null', state=1, createtime=null, updateTime=null, user_id=0}, DeliveryMan{name='null', phone='2222', area='null', officeholding='null', jobnum='null', state=2, createtime=null, updateTime=null, user_id=0}]
om.fasterxml.jackson.databind.ObjectMapper
[DeliveryMan{name='null', phone='1111', area='null', officeholding='null', jobnum='null', state=1, createtime=null, updateTime=null, user_id=0}, DeliveryMan{name='null', phone='2222', area='null', officeholding='null', jobnum='null', state=2, createtime=null, updateTime=null, user_id=0}]
 

三、参考文章

jackson完成json和对象/map/list互转:https://blog.csdn.net/qq_37936542/article/details/79268402


fastjson List<> 转Json , Json 转List<>:https://www.cnblogs.com/xiaohouzai/p/8972286.html

相关文章
|
9月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
472 0
|
JSON JavaScript Java
在Java中处理JSON数据:Jackson与Gson库比较
本文介绍了JSON数据交换格式及其在Java中的应用,重点探讨了两个强大的JSON处理库——Jackson和Gson。文章详细讲解了Jackson库的核心功能,包括数据绑定、流式API和树模型,并通过示例演示了如何使用Jackson进行JSON解析和生成。最后,作者分享了一些实用的代码片段和使用技巧,帮助读者更好地理解和应用这些工具。
748 0
在Java中处理JSON数据:Jackson与Gson库比较
|
JSON 安全 fastjson
高性能 JSON 处理:为何选择 Fastjson?
Fastjson 是由阿里巴巴集团开发的一个高性能的 JSON 处理库,它支持 Java 对象与 JSON 字符串之间的互相转换。
1784 0
高性能 JSON 处理:为何选择 Fastjson?
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
1967 7
|
JSON Java API
Jackson:SpringBoot中的JSON王者,优雅掌控数据之道
【8月更文挑战第29天】在Java的广阔生态中,SpringBoot以其“约定优于配置”的理念,极大地简化了企业级应用的开发流程。而在SpringBoot处理HTTP请求与响应的过程中,JSON数据的序列化和反序列化是不可或缺的一环。在众多JSON处理库中,Jackson凭借其高效、灵活和强大的特性,成为了SpringBoot中处理JSON数据的首选。今天,就让我们一起深入探讨Jackson如何在SpringBoot中优雅地控制JSON数据。
480 0
|
JSON 安全 fastjson
FastJSON库:JSON处理效率与安全性评估
FastJSON库:JSON处理效率与安全性评估
|
2月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
3月前
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。