JSON and POJO Mutual transfer,list<t> and JSON

Source: Internet
Author: User

JSON and POJO
    Import Com.alibaba.fastjson.JSONObject;    Import Org.slf4j.Logger;    Import Org.slf4j.LoggerFactory;        /** * Object Pojo and JSON cross-transfer * * @author MXN * @create 2018-09-05 14:35 */public class Jsonutil {        public static final Logger Logger = Loggerfactory.getlogger (Jsonutil.class);         /** * JSON to POJO * @param POJO * @param tclass * @param <T> * @return */public static <T> T getObject (String pojo, class<t> tclass) {try {ret            Urn Jsonobject.parseobject (Pojo, Tclass);            } catch (Exception e) {logger.error (Tclass + "Go JSON failed");        } return null;        }/** * Pojo to JSON * @param tresponse * @param <T> * @return * *            public static <T> string Getjson (T tresponse) {string pojo = jsonobject.tojsonstring (tresponse);    return Pojo;    }    } 
List and 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);    }}
Test Pojo and 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 and 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 and POJO,list<t> and JSON cross-transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.