FastJson的使用

下载 fastjson.jar https://search.maven.org/remote_content?g=com.alibaba&a=fastjson&v=LATEST
补充连接: Spring集成Fast json ,替换Spring MVC 默认的HttpMessageConverter 解析json https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson

  1. JavaBean 转 JSON String
    @Test
    public void testFastJson(){
        Customer customer = new Customer();
        customer.setCust_address("地址");
        customer.setCust_create_id(11L);
        customer.setCust_industry("行业");
        String s = com.alibaba.fastjson.JSON.toJSONString(customer);
        System.out.println(s);
    }

输出

{"cust_address":"地址","cust_create_id":11,"cust_industry":"行业"}

注意: fastjson 不会将JavaBean对象中为 null 的属性和值 转换到 字符串中, 但json-lib会

  1. JsonString => JavaBean

    @Test
    public void testStringToBean(){
        String str ="{\"cust_address\":\"地址\",\"cust_create_id\":11,\"cust_industry\":\"行业\"}";
        Customer customer = com.alibaba.fastjson.JSON.parseObject(str,Customer.class);
        System.out.println(customer);
    }

输出:

Customer{cust_id=null, cust_name='null', cust_user_id=null, cust_create_id=11, cust_source='null', cust_industry='行业', cust_level='null', cust_linkman='null', cust_phone='null', cust_mobile='null', cust_zipcode='null', cust_address='地址', cust_createtime=null}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • @synthesize和@dynamic分别有什么作用?@property有两个对应的词,一个是 @synthes...
    笔笔请求阅读 533评论 0 1
  • 猜想runloop内部是如何实现的?一般来讲,一个线程一次只能执行一个任务,执行完成后线程就会退出。如果我们需要一...
    笔笔请求阅读 443评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 在分析大量的开源Java项目源码时候,我发现Java开发者经常使用两种方式进行排序。一个是使用Collection...
    尧字节阅读 221评论 0 1
  • 听《翻转课堂的可汗学院》,对可汗学院和翻转式学习多了些了解。印象深的几点: 1.精熟学习。每个知识点都能清楚理解,...
    阿中2016阅读 638评论 0 0