JSON 和 POJO 互转,List<T> 和 JSON 互转

梦想与她 提交于 2020-02-13 18:43:34

JSON 和 POJO

    import com.alibaba.fastjson.JSONObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    /**
     * 对象POJO和JSON互转
     *
     * @author mxn
     * @create 2018-09-05 14:35
     */
    
    public class JsonUtil {

        public static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);

        /**
         * JSON 转 POJO
         * @param pojo
         * @param tClass
         * @param <T>
         * @return
         */
        public static <T> T getObject(String pojo, Class<T> tClass) {
            try {
                return JSONObject.parseObject(pojo, tClass);
            } catch (Exception e) {
                logger.error(tClass + "转 JSON 失败");
            }
            return null;
        }

        /**
         * pojo 转json
         * @param tResponse
         * @param <T>
         * @return
         */
        public static <T> String getJson(T tResponse) {
            String pojo = JSONObject.toJSONString(tResponse);
            return pojo;
        }
    }

List 和 JSON

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.List;

/**
 * List集合和JSON互转工具类
 * @author mxn
 * @create 2018-09-05 14:45
 */

public class JsonListUtil {

    /**
     * List<T> 转 json
     * @param ts
     * @param <T>
     * @return
     */
    public static <T> String listToJson(List<T> ts){
        return JSON.toJSONString(ts);
    }

    /**
     * json 转 List<T>
     * @param jsonStr
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(String jsonStr, Class<T> tClass) {
        return JSONArray.parseArray(jsonStr, tClass);
    }

}

测试

POJO和JSON

import org.junit.Test;
import org.seckill.entity.Seckill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author mxn
 * @create 2018-09-05 14:40
 */
public class JsonUtilTest {

    public static final Logger logger = LoggerFactory.getLogger(JsonUtilTest.class);
    
    @Test
    public void method1() {
        Seckill seckill = new Seckill();
        seckill.setName("Iphone X");
        String json = JsonUtil.getJson(seckill);
        logger.info("JsonUtilTest.method1 info={}",json);

        Seckill object = JsonUtil.getObject(json, Seckill.class);
        logger.info("JsonUtilTest.method1 info={}",object.getName());
    }

}

List 和 JSON

import org.junit.Before;
import org.junit.Test;
import org.seckill.entity.Seckill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mxn
 * @create 2018-09-05 14:48
 */
public class JsonListUtilTest {
    
    public static final Logger logger = LoggerFactory.getLogger(JsonListUtilTest.class);
    

    private List<Seckill> list = new ArrayList<Seckill>();

    private String jsonStr;

    @Before
    public void beforeMethod() {
        for (int i = 0; i < 5; i++) {
            Seckill seckill = new Seckill();
            seckill.setName("iphone " + i);
            list.add(seckill);
        }
        jsonStr = "[{\"name\":\"iphone 0\",\"number\":0,\"seckillId\":0},{\"name\":\"iphone 1\",\"number\":0,\"seckillId\":0},{\"name\":\"iphone 2\",\"number\":0,\"seckillId\":0},{\"name\":\"iphone 3\",\"number\":0,\"seckillId\":0},{\"name\":\"iphone 4\",\"number\":0,\"seckillId\":0}]";
    }

    @Test
    public void method() {
        String s = JsonListUtil.listToJson(list);
        logger.info("JsonListUtilTest.method info={}",s);
    }

    @Test
    public void method2() {
        List<Seckill> seckills = JsonListUtil.jsonToList(jsonStr, Seckill.class);
        for (Seckill seckill : seckills) {
            logger.info("JsonListUtilTest.method2 info={}",seckill.getName());
        }
    }

}

POJO

import java.util.Date;

/**
 * @author mxn
 * @create 2018-05-17 15:36
 */

public class Seckill {

    private long seckillId;

    private String name;

    private int number;

    private Date startTime;

    private Date endTime;

    private Date createTime;

    public long getSeckillId() {
        return seckillId;
    }

    public void setSeckillId(long seckillId) {
        this.seckillId = seckillId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "Seckill{" +
                "seckillId=" + seckillId +
                ", name='" + name + '\'' +
                ", number=" + number +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", createTime=" + createTime +
                '}';
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!