標籤: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 互轉