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

來源:互聯網
上載者:User

標籤:json.js   factor   entity   art   getc   creat   star   getname   nal   

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 +                ‘}‘;    }}

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.