java常用数据类型相互转换

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();
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的蚂蚁【你若】

如果帮助到了您,一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值