fastjson版本:https://repo1.maven.org/maven2/com/alibaba/fastjson/1.1.15/
JsonKit.java
package com.yilabao.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yilabao.test.User;
public class JsonKit {
/**
* 通过json重置java对象
*
* @param obj
* @param c
* @param <T>
* @return
*/
public static <T> T resetObject(T obj, Class<T> c) {
JSONObject jsonObj = (JSONObject) JSON.toJSON(obj);
jsonObj.put("pic", "http://www.xxx.xxx/" + jsonObj.get("pic"));
return JSONObject.toJavaObject(jsonObj, c);
}
public static void main(String[] args) {
User user = new User();
user.setPic("file/test.png");
User newUser = JsonKit.resetObject(user, User.class);
System.out.println(JSON.toJSONString(newUser));
}
}
User.java
package com.yilabao.test;
public class User {
private String pic;
public User() {
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}