两个对象值转换的方法(BeanUtils.copyProperties与JSONObject.parseObject对比)


将源对象赋值到目标对象方法:
方法一:BeanUtils.copyProperties(源对象, 目标对象); //org.springframework.beans.BeanUtils
方法二:目标对象 =JSONObject.parseObject(JSON.toJSONString(源对象), 目标对象.class); //com.alibaba.fastjson.JSON; com.alibaba.fastjson.JSONObject;

实测结果方法一的耗时为方法二的2-3倍。

 

附测试代码:

public class TestM {
public static void main(String[] args) {
Testb testa=new Testb();
Testa testb=new Testa();
long a=System.currentTimeMillis();
BeanUtils.copyProperties(testa, testb);
System.err.println("方法一耗时:"+(System.currentTimeMillis()-a));

long b=System.currentTimeMillis();
Testa testc= JSONObject.parseObject(JSON.toJSONString(testb), Testa.class);
System.err.println("方法二耗时:"+(System.currentTimeMillis()-b));

}

测试结果:
方法一耗时:214
方法二耗时:87

关于两者的差异因为没有看到alibaba的JSONObject.parseObject方法的源码而告终,希望各位看官能在评论多多讲解。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM