zoukankan      html  css  js  c++  java
  • 阿里巴巴FastJSON使用实例

     

    1. 什么是fastjson?

    fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。

    Fastjson使用场景

    Fastjson已经被广泛使用在各种场景,包括cache存储、RPC通讯、MQ通讯、网络协议通讯、Android客户端、Ajax服务器处理程序等等。

    2.fastjson的优点

    2.1 速度快

    fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。

    2.2 使用广泛

    fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。

    2.3 测试完备

    fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。

    2.4 使用简单

    fastjson的API十分简洁。

    String text = JSON.toJSONString(obj); //序列化
    VO vo = JSON.parseObject("{...}", VO.class); //反序列化
    

    2.5 功能完备

    支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。

    以下例子对JSON.toJSONString和JSON.parseObject这2个重要方法进行测试:

    Fruit实体类:

     1 package com.led.daorumysql;
     2 
     3 /**
     4  * 定义水果实体类
     5  * @author 86157
     6  *
     7  */
     8 public class Fruit {
     9     
    10     //水果名字
    11     private String name;
    12     //水果产地
    13     private String origin;
    14     //水果价格
    15     private Integer price;
    16     
    17     public String getName() {
    18         return name;
    19     }
    20     public void setName(String name) {
    21         this.name = name;
    22     }
    23     public String getOrigin() {
    24         return origin;
    25     }
    26     public void setOrigin(String origin) {
    27         this.origin = origin;
    28     }
    29     public Integer getPrice() {
    30         return price;
    31     }
    32     public void setPrice(Integer price) {
    33         this.price = price;
    34     }
    35     @Override
    36     public String toString() {
    37         return "Fruit [name=" + name + ", origin=" + origin + ", price=" + price + "]";
    38     }
    39     
    40     public Fruit(String name, String origin, Integer price) {
    41         super();
    42         this.name = name;
    43         this.origin = origin;
    44         this.price = price;
    45     }
    46     
    47     public Fruit() {
    48         super();
    49     }
    50     
    51     
    52     
    53     
    54 }

    主类:

     1 package com.led.daorumysql;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import com.alibaba.fastjson.JSON;
     7 
     8 /**
     9  * FastJSON测试类
    10  * @author 86157
    11  *
    12  */
    13 public class FastJSONTest {
    14 
    15     public static void main(String[] args) {
    16         //实例化Fruit类
    17         Fruit fruitOne = new Fruit("apple", "America", 13);
    18         Fruit fruitTwo = new Fruit("orange", "China", 15);
    19         //创建fruitList对象
    20         List<Fruit> fruitList = new ArrayList<Fruit>();
    21         fruitList.add(fruitOne);
    22         fruitList.add(fruitTwo);
    23         
    24         //将java对象(Fruit)转成json字符串
    25         System.out.println("=========将java对象(Fruit)转成json字符串===========");
    26         String fruitOne_jsonString = JSON.toJSONString(fruitOne);
    27         System.out.println("fruitOne_jsonString: " + fruitOne_jsonString);
    28         System.out.println();
    29         
    30         //将json字符串转成java对象(Fruit)
    31         System.out.println("=========将json字符串转成java对象(Fruit)===========");
    32         Fruit fruitOne_parseObject = JSON.parseObject(fruitOne_jsonString, Fruit.class);
    33         System.out.println("fruit object: " + fruitOne_parseObject);
    34         System.out.println("fruit name: " + fruitOne_parseObject.getName());
    35         System.out.println("fruit origin: " + fruitOne_parseObject.getOrigin());
    36         System.out.println("fruit price: " + fruitOne_parseObject.getPrice());
    37         System.out.println();
    38         
    39         //将java对象(fruitList)转成json字符串
    40         System.out.println("=========将java对象(fruitList)转成json字符串========");
    41         String fruitList_jsonString = JSON.toJSONString(fruitList);
    42         System.out.println("fruitList_jsonString: " + fruitList_jsonString);
    43         System.out.println();
    44         
    45         //将json字符串转成java对象(fruitList)
    46         System.out.println("=========将json字符串转成java对象(fruitList)========");
    47         List<Fruit> parseArray = JSON.parseArray(fruitList_jsonString, Fruit.class);
    48         System.out.println("fruitOne object: " + parseArray.get(0).toString());
    49         System.out.println("fruitTwo object: " + parseArray.get(1).toString());
    50         System.out.println("fruitTwo object name: " + parseArray.get(1).getName());
    51         
    52     }
    53 }

    控制台输出:

     1 =========将java对象(Fruit)转成json字符串===========
     2 fruitOne_jsonString: {"name":"apple","origin":"America","price":13}
     3 
     4 =========将json字符串转成java对象(Fruit)===========
     5 fruit object: Fruit [name=apple, origin=America, price=13]
     6 fruit name: apple
     7 fruit origin: America
     8 fruit price: 13
     9 
    10 =========将java对象(fruitList)转成json字符串========
    11 fruitList_jsonString: [{"name":"apple","origin":"America","price":13},{"name":"orange","origin":"China","price":15}]
    12 
    13 =========将json字符串转成java对象(fruitList)========
    14 fruitOne object: Fruit [name=apple, origin=America, price=13]
    15 fruitTwo object: Fruit [name=orange, origin=China, price=15]
    16 fruitTwo object name: orange

    注意:

    实体类中要创建构造方法,不然parseObject会报如下错误:

    Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.led.daorumysql.Fruit
        at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:197)
        at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:465)
        at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:412)
        at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:324)
        at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:594)
        at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:569)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:257)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:186)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:310)
        at com.led.daorumysql.FastJSONTest.main(FastJSONTest.java:27)
  • 相关阅读:
    koa2 ctx.body 在 mysql query的回调函数中无法赋值的问题
    hibernate 实现多表连接查询
    Struts2 的国际化实现
    struts2 dwr There is no Action mapped for action ... 原因及解决方案
    Hibernate4.1配置数据库连接池 org.hibernate.service.jndi.JndiException:Unable to lookup JNDI name java:comp/env...
    Android 使用JSON格式与服务器交互 中文乱码问题解决
    Struts2 访问 Servlet API 的三种方法
    Struts2 输入校验
    hibernate4 和 spring3 整合注意事项 否则java.lang.NoSuchMethodError异常
    MySQL密码忘记的解决方案
  • 原文地址:https://www.cnblogs.com/stm32stm32/p/9001715.html
Copyright © 2011-2022 走看看