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();
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/143383.html