美文网首页
FastJson的使用

FastJson的使用

作者: Ethan_Walker | 来源:发表于2017-08-28 19:58 被阅读39次

下载 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}

相关文章

网友评论

      本文标题:FastJson的使用

      本文链接:https://www.haomeiwen.com/subject/fyngdxtx.html