【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
一般来说两个对象的属性复制用BeanUtil的 copyXXXPropter 方法 即可。 就可以达到这个效果了。
但是如果是 json 对象的复制呢? 这个方法就不适用了。(比如我想要复制的是 某个key 里面的jsonObject 的属性就不可以了)
没找到 有工具类写过这个,于是 自己写了 一个
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
* @version 1.0
* @Description: fastjson 属性复制
* @Author: oumin
* @Date: 2019-12-13 11:30
*/
public class JsonObjectCopyUtil {
/**
* json 对象的属性复制。浅复制
* 如果 target 存在了对应的key 那么就对其内容进行从source 里面复制。
* 如果 target 不存在对应的 key ,而 source里面有,则 从source里面取 出来
* @param source 被复制的员
* @param target 目标
*/
public static void copyKeyValueTwoJsonObject(JSONObject source, JSONObject target){
if (source == null || target==null) {
return;
}
for (Map.Entry<String, Object> senty : source.entrySet()) {
String skey=senty.getKey();
Object sValue=senty.getValue();
if (!target.containsKey(skey)){
// 目标没有对应的key , 则 全部从 源 获取
target.put(skey,sValue);
continue;
}
//判空
if (sValue == null) {
continue;
}
if (target.get(skey)==null){
//目标内容为空,全部取源的
target.put(skey,sValue);
continue;
}
if (sValue instanceof JSONObject &&
!( target.get(skey) instanceof JSONObject ) ){
//源 value 是 jsonObject ,而 目标不是,全部复制源的
target.put(skey,sValue);
continue;
}
if (sValue instanceof JSONObject &&
( target.get(skey) instanceof JSONObject ) ){
//源 value 是 jsonObject ,而 目标也是,递归
JsonObjectCopyUtil.copyKeyValueTwoJsonObject((JSONObject) sValue,target.getJSONObject(skey));
continue;
}
if (sValue instanceof JSONArray
&& !( target.get(skey) instanceof JSONArray ) ){
//源 value 是 jsonArray ,但是目标不是,全部复制源的
target.put(skey,sValue);
continue;
}
if (sValue instanceof JSONArray
&& ( target.get(skey) instanceof JSONArray ) ){
//源 value 是 jsonArray ,但是目标也是
JSONArray sAy= (JSONArray) sValue;
JSONArray tAy=target.getJSONArray(skey);
if (sAy.isEmpty()){
//源 array 为空
continue;
}
if (tAy.isEmpty()){
//目标的对应为空,放入源的array
target.put(skey,sValue);
continue;
}
JsonObjectCopyUtil.copyKeyValueTwoJsonArray(sAy,tAy);
continue;
}
// 最后 放入源的,不管目标的怎样
target.put(skey,sValue);
}
}
/**
* 浅复制
* 两个 jsonArray 复制 key ,value
* @param source
* @param target
*/
public static void copyKeyValueTwoJsonArray(JSONArray source, JSONArray target){
if (source.isEmpty()){
//源 array 为空
return;
}
if (target.isEmpty()){
//目标的对应为空,放入源的array
target=source;
return;
}
Object sFirstObj=source.get(0);
Object tFristObj=target.get(0);
boolean isJsonObjetOrJsonArray=
( sFirstObj instanceof JSONObject || sFirstObj instanceof JSONArray )
&& ( tFristObj instanceof JSONObject || tFristObj instanceof JSONArray);
if (!isJsonObjetOrJsonArray){
// array 内容 只是普遍的对象,直接 复制即可.
target=source;
return;
}
if (source.size() != target.size()){
// TODO 可以继续 改进,如果 长度不一致 根据不同策略进行处理。但是很多情况下到这里已经够用了
//目标的 array 长度 与源的不一致,取源的
target=source;
return;
}
// 长度一致,那么 每个内容进行 复制
for (int i = 0; i < source.size(); i++) {
//递归 复制
Object sindexObj=source.get(i);
Object tindexObj=target.get(i);
if (sindexObj==null){
continue;
}
if (tindexObj==null){
target.add(i,sindexObj);
continue;
}
if (sindexObj instanceof JSONObject &&
tindexObj instanceof JSONObject){
//递归
JsonObjectCopyUtil.copyKeyValueTwoJsonObject((JSONObject) sindexObj,(JSONObject)tindexObj);
continue;
}
if (sindexObj instanceof JSONArray &&
tindexObj instanceof JSONArray){
//类型json array 递归
JsonObjectCopyUtil.copyKeyValueTwoJsonArray((JSONArray) sindexObj,(JSONArray) tindexObj);
continue;
}
target.add(i,sindexObj);
}
}
}
来源:oschina
链接:https://my.oschina.net/ouminzy/blog/3142624