package eshop.common.util;
import com.alibaba.fastjson.JSON;
import eshop.common.entity.Entity;
import java.util.ArrayList;
import java.util.List;
public class JSONUtil {
public static void main(String[] args) {
Entity entity = new Entity();
entity.setCreateTime("22:04");
entity.setId("id");
String json = entity2JSON(entity);
List<Entity> entityList = new ArrayList<>();
entityList.add(entity);
String json1 = entityList2JSON(entityList);
System.out.println(json1);
String str = "{\"createTime\":\"22:04\",\"id\":\"id\",\"isDel\":\"1\"}";
Entity entity1 = JSON2Entity(str, Entity.class);
System.out.println(entity1.getCreateTime());
String jsonArray = "[{\"createTime\":\"22:11\",\"id\":\"id\",\"isDel\":\"1\"}," +
"{\"createTime\":\"22:04\",\"id\":\"id\",\"isDel\":\"1\"}]";
List<Entity> entityList1 = JSONArray2List(jsonArray, Entity.class);
System.out.println(entityList1.get(0).getCreateTime());
}
/**
*
* @param entity
* @return
*/
public static String entity2JSON(Object entity){
return JSON.toJSONString(entity);
}
public static String entityList2JSON(List<?> entityList){
return JSON.toJSONString(entityList);
}
public static <T> T JSON2Entity(String json, Class<T> clazz){
return JSON.parseObject(json, clazz);
}
public static <T> List<T> JSONArray2List(String json, Class<T> clazz){
return JSON.parseArray(json, clazz);
}
}