Json与对象的相互转换

本文介绍了如何使用Jackson库的ObjectMapper类进行Java对象与JSON的相互转换。通过示例展示了单个对象和集合对象的转换过程,并提供了一个ObjectMapper工具类的实现,方便进行JSON操作。
摘要由CSDN通过智能技术生成

Json与对象的相互转换

利用ObjectMapper类 com.fasterxml.jackson.databind.ObjectMapper;

package com.jt.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.pojo.ItemDesc;
import org.junit.jupiter.api.Test;
import java.util.Date;

public class TestObjrctMapper {
    @Test
    public void text01() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();//
        ItemDesc itemDesc = new ItemDesc();//简单对象 有get方法   set方法(返回值是当前对象,链式加载)
        itemDesc.setItemDesc("商品详情").setItemId(1545l).setCreated(new Date()).setUpdated(new Date());
        //将对象转换为json
        String json = objectMapper.writeValueAsString(itemDesc);//
        System.out.println(json);//输出json
        //json转换为对象
        ItemDesc itemDesc1 = objectMapper.readValue(json,ItemDesc.class);//参数json对象,对象模型
        System.out.println(itemDesc1.getItemDesc());//输出对象里的一个信息,验证
    }
}

运行结果:
在这里插入图片描述

json和集合之间的转换

 @Test
    public void test02()throws JsonProcessingException {
        ObjectMapper objectMapper1 = new ObjectMapper();
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemDesc("商品详情").setItemId(1545l).setCreated(new Date()).setUpdated(new Date());
        ItemDesc itemDesc1 = new ItemDesc();
        itemDesc.setItemDesc("商品详情1").setItemId(15451l).setCreated(new Date()).setUpdated(new Date());
        List<ItemDesc> lists = new ArrayList<>();
        lists.add(itemDesc);
        lists.add(itemDesc1);
        //将集合转换为json
        String json1 = objectMapper1.writeValueAsString(lists);
        System.out.println(json1);
        //将json字符串转换为对象       这里的对象模型为可以为List.class接口  或者lists.getClass()
        List lists1 = objectMapper1.readValue(json1, lists.getClass());
        System.out.println(lists1);
    }

运行结果 json字符串值之间是用 : 对象的值之间是用 = 号
在这里插入图片描述

创建ObjectMapper的工具类

package com.jt.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperUtil {
    /**
     * 1.将用户传递的对象转换为json字符串
     * 2.将用户传递的json字符串转换为对象
     */
    private static final ObjectMapper  OMAPPER= new ObjectMapper();
    //将对象转换为字符串
    public static String toJSON(Object object){
        if(object == null){
            throw  new RuntimeException("传递的值为null.经检查");
        }
        try {
            String json = OMAPPER.writeValueAsString(object);
            return json;
        } catch (JsonProcessingException e) {
            //将检查异常转换为运行时异常
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    //将用户传递的json字符串转换为对象   用户传递什么类型,我就返回什么类型
        public static <T> T toObj(String json,Class<T> targer){
        if(json==null||"".equals(json)){
            throw  new RuntimeException("传递的值为null.经检查");
        }
            try {
                return OMAPPER.readValue(json,targer);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

        }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值