java基础之泛型对象与json互转
1. 场景描述
把泛型对象转成字符串放到缓存中,获取后使用有点问题,记录下,有碰到的朋友,参考下。
2. 解决方案
2.1 操作类及说明
/**
* @auther: 软件老王
*/
public static void main(String[] args) {
VO<UserParm> vo = new VO<UserParm>();
UserParm user = new UserParm();
user.setUserIdName("1111");
user.setMobilePhoneNo("2222");
vo.setData(user);
//正常情况
//1.对象转json str
String str = JSONObject.toJSONString(vo);
System.out.println(str);
//2,json str 转对象
VO vojs= JSONObject.parseObject(str,VO.class);
System.out.println(vojs);
//泛型 不能使用强转,不管用,报错
VO<UserParm> vojs2= JSONObject.parseObject(str,VO.class);
System.out.println(vojs2);
// System.out.println(vojs2.getData().getMobilePhoneNo());
//泛型,正常转换
VO<UserParm> vojs3= JSONObject.parseObject(str,new TypeReference<VO<UserParm>>(){});
System.out.println(vojs3.getData().getMobilePhoneNo());
}
说明:
(1) 用的是阿里的fastjson,pom文件中增加下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
(2)对象转json格式str
String str = JSONObject.toJSONString(vo); //软件老王
(3)json格式str转对象
VO vojs= JSONObject.parseObject(str,VO.class); //软件老王
(4)泛型的不一样,转换为泛型对象UserParm的时候会报错。
System.out.println(vojs2.getData().getMobilePhoneNo());
com.alibaba.fastjson.JSONObject cannot be cast to com.laowang.spcrud.entity.UserPar
泛型的需要使用TypeReference才行:
VO<UserParm> vojs3= JSONObject.parseObject(str,new TypeReference<VO<UserParm>>(){});
(5)结果展示
com.laowang.spcrud.service.TestService
{"code":"0","data":{"mobilePhoneNo":"2222","userIdName":"1111"}}
com.laowang.spcrud.entity.VO@368239c8
com.laowang.spcrud.entity.VO@9e89d68
2222
Process finished with exit code 0
2.2 两个对象类
(1)泛型类VO
package com.laowang.spcrud.entity;
import java.io.Serializable;
/**
* @auther: 软件老王
*/
public class VO<T> implements Serializable {
public static final String SUCCESS = "0";
public static final String FAIL = "1";
private static final long serialVersionUID = 1L;
private String code = SUCCESS;
private String msg;
private T data;
public static String getSUCCESS() {
return SUCCESS;
}
public static String getFAIL() {
return FAIL;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
(2)实体类UserParm
package com.laowang.spcrud.entity;
import java.io.Serializable;
/**
* @auther: 软件老王
*/
public class UserParm implements Serializable {
private static final long serialVersionUID = 1L;
private String mobilePhoneNo;
private String email;
private String uid;
private String userIdName;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getMobilePhoneNo() {
return mobilePhoneNo;
}
public void setMobilePhoneNo(String mobilePhoneNo) {
this.mobilePhoneNo = mobilePhoneNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUserIdName() {
return userIdName;
}
public void setUserIdName(String userIdName) {
this.userIdName = userIdName;
}
}
I’m 「软件老王」,如果觉得还可以的话,关注下呗,后续更新秒知!欢迎讨论区、同名公众号留言交流!
更多信息请关注公众号:「软件老王」,关注不迷路,IT技术与相关干货分享,回复关键字获取对应干货,本文版权归作者软件老王所有,转载需注明作者、超链接,否则保留追究法律责任的权利。