web项目---fastjson更加fast的json解析器

在我之前的博客中,讨论到了GSON用来将Javabean和json之间的转化,这样方便的完成前后台数据交互的统一,但是在后来我用Java写爬虫时,又发现了一个比gson更强大的fastjson,他的对Java的解析效率更好,比Gson更好用

FastJson

这时阿里巴巴下的一个开源框架,可以用来对json数据的解析,它和它的名字一样,就是快,在功能强大的同时,还不依赖其他的任何类库,它主要以下的三个类:

  • JSON:fastjson的解析器,用于JSON数据字符串与JSON对象以及JavaBean之间的转换
  • JSONObject:提供的一个json对象
  • JSONArray:fastJson提供的json的数组对象

接下来就是对它使用的讲解

首先,我们在maven的配置文件pom.xml中导入它的依赖:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.23</version>
</dependency>

序列化
序列化就是指 把JavaBean对象转成JSON格式的字符串

String objJson = JSON.toJSONString(Object object); //传入一个对象,将对象转成JSON字符串

反序列化
反序列化就是把JSON格式的字符串转化为Java Bean对象。

Person person = JSON.parseObject(personJson, Person .class);

  • JSONObject,JSONArray是JSON的两个子类
  • JSONObject相当于Map<String, Object>
  • JSONArray相当于List

实例

我们需要先定义几个json格式的字符串

//json字符串-简单对象型
private static final String JSON_OBJ_STR = “{“studentName”:“lily”,“studentAge”:12}”;

//json字符串-数组类型
private static final String JSON_ARRAY_STR = “[{“studentName”:“lily”,“studentAge”:12},{“studentName”:“lucy”,“studentAge”:15}]”;

//复杂格式json字符串
private static final String COMPLEX_JSON_STR = “{“teacherName”:“crystall”,“teacherAge”:27,“course”:{“courseName”:“english”,“code”:1270},“students”:[{“studentName”:“lily”,“studentAge”:12},{“studentName”:“lucy”,“studentAge”:15}]}”;

json字符串-简单对象型与JSONObject之间的转换:

/**
 * json字符串-简单对象型到JSONObject的转换
 */
@Test
public void testJSONStrToJSONObject() {
 
    JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
 
    System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
            + jsonObject.getInteger("studentAge"));
 
}
 
/**
 * JSONObject到json字符串-简单对象型的转换
 */
@Test
public void testJSONObjectToJSONStr() {
 
    //已知JSONObject,目标要转换为json字符串
    JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
    // 第一种方式
    String jsonString = JSONObject.toJSONString(jsonObject);
 
    // 第二种方式
    //String jsonString = jsonObject.toJSONString();
    System.out.println(jsonString);
}

json字符串-简单对象型与javaBean之间的转换

/**
 * json字符串-简单对象到JavaBean之间的转换
 */
@Test
public void testJSONStrToJavaBeanObj() {
 
    //第一种方式
    JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
 
    String studentName = jsonObject.getString("studentName");
    Integer studentAge = jsonObject.getInteger("studentAge");
 
    //Student student = new Student(studentName, studentAge);
 
    //第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
    //Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
 
    //第三种方式,使用Gson的思想
    Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
 
    System.out.println(student);
}
 
/**
 * JavaBean到json字符串-简单对象的转换
 */
@Test
public void testJavaBeanObjToJSONStr() {
 
    Student student = new Student("lily", 12);
    String jsonString = JSONObject.toJSONString(student);
    System.out.println(jsonString);
}

简单javaBean与json对象之间的转换

/**
 * 简单JavaBean_obj到json对象的转换
 */
@Test
public void testJavaBeanToJSONObject(){
 
    //已知简单JavaBean_obj
    Student student = new Student("lily", 12);
 
    //方式一
    String jsonString = JSONObject.toJSONString(student);
    JSONObject jsonObject = JSONObject.parseObject(jsonString);
    System.out.println(jsonObject);
 
    //方式二
    JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student);
    System.out.println(jsonObject1);
}
 
/**
 * 简单json对象到JavaBean_obj的转换
 */
@Test
public void testJSONObjectToJavaBean(){
 
    //已知简单json对象
    JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
 
    //第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
    Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {});
    System.out.println(student);
 
    //第二种方式,使用Gson的思想
    Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
    System.out.println(student1);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值