1. 对象转Map
Map map= JSON.parseObject(JSON.toJSONString(entity),Map.class);
System.out.println(map.toString());
2. 对象转json
com.alibaba.fastjson.JSONObject
JSONObject jsonObject= (JSONObject) JSON.toJSON(entity);
3. map转对象
Map map=new HashMap();
map.put("nickName","努力的蚂蚁");
TUsersDo queryDo = JSON.parseObject(JSON.toJSONString(map), TUsersDo.class);
4. json转对象
JSONObject jsonObject=new JSONObject();
jsonObject.put("nickName","努力的蚂蚁");
TUsersDo tUsersDo=JSON.parseObject(jsonObject.toJSONString(),TUsersDo.class);
5. map转json
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "a");
map.put("b", "123");
JSONObject json = new JSONObject(map);
6. date转字符串
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
7. json字符串转json对象
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
8. list转JsonArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
9. JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
10. String转JSONArray
String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{name:Lily,age:15,sex:female}]";
JSONArray tableData = JSONArray.parseArray(st);
11. 数组转list
String result=resultUtils.getData().toString();
String arrays[]=result.split("\\,");
12. 字符串转date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
try {
logDomain.setUpdateTime(sdf.parse(newestUpdateTime));
} catch (ParseException e) {
e.printStackTrace();
}