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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值