FastJson、Jackson、Gson、Json的效率简单对比

本文介绍了在优化代码过程中遇到的JSON解析效率问题,对比了FastJson、Jackson、Gson和org.Json四个Java JSON库在处理大量数据时的性能。测试结果显示,对于str—>list转换,FastJson速度最快,Gson次之但不稳定,org.Json最慢;而对于list—>str转换,Jackson表现最佳,其他三者差距较小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天上班优化代码的时候,发现从Redis中取出List<Map<String,String>>格式的json字符串,通过net.sf.json将其转成List的时候,如果List.size()在1000左右时,消耗处理时间为1000ms,效率非常低;然后搜了下java解析json常用api,发现alibaba的fastJson处理效率还是不错的,当size在1000左右时,也就不足100ms。
然后。。。。回家就简单对比了下几个json处理方式:FastJson、Jackson、Gson、org.Json;
首先maven引入依赖:

    <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0.pr4</version>
        </dependency>

编写相应的类,测试jsonStr–list,list–jsonStr的处理时间

  • org.json
package com.love.yu.maven.learn.json;

import java.util.List;
import org.json.JSONArray;

public class TestJson {
    public void testJsonStr2List(String str) {
        long a = System.currentTimeMillis();
        JSONArray jsonArray = new JSONArray(str);
        List<Object> list = jsonArray.toList();
        long b = System.currentTimeMillis();
        System.out.println("JsonStr2List:size=" + list.size() + ",time=" + (b - a));
    }

    public String testJsonList2Str(List<String> list) {
        long a = System.currentTimeMillis();
        JSONArray array = new JSONArray(list);
        String str = array.toString();
        long b = System.currentTimeMillis();
        System.out.println("JsonList2Str:length=" + str.length() + ",time=" + (b - a));
        return str;
    }
}
  • fastJson
package com.love.yu.maven.learn.json;

import java.util.List;
import com.alibaba.fastjson.JSONArray;

public class TestFastJson {
    public void testFastJsonStr2List(String str) {
        long a = System.currentTimeMillis();
        JSONArray jsonArray = JSONArray.parseArray(str);
        List<String> list = jsonArray.toJavaList(String.class);
        long b = System.currentTimeMillis();
        System.out.println("FastJsonStr2List:size=" + list.size() + ",time=" + (b - a));
    }

    public String testFastJsonList2Str(List<String> list) {
        long a = System.currentTimeMillis();
        String str = JSONArray.toJSONString(list);
        long b = System.currentTimeMillis();
        System.out.println("FastJsonList2Str:length=" + str.length() + ",time=" + (b - a));
        return str;
    }
}
  • jackson
package com.love.yu.maven.learn.json;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJackson {
    public void testJacksonStr2List(String str) throws JsonParseException, JsonMappingException, IOException {
        long a = System.currentTimeMillis();
        ObjectMapper objectMapper = new ObjectMapper();
        List<String> list = objectMapper.readValue(str, List.class);
        long b = System.currentTimeMillis();
        System.out.println("JacksonStr2List:size=" + list.size() + ",time=" + (b - a));
    }

    public String testJacksonList2Str(List<String> list) throws JsonProcessingException {
        long a = System.currentTimeMillis();
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(list);
        long b = System.currentTimeMillis();
        System.out.println("JacksonList2Str:length=" + str.length() + ",time=" + (b - a));
        return str;
    }
}
  • gson
package com.love.yu.maven.learn.json;

import java.util.List;

import com.google.gson.Gson;

public class TestGson {
    public void testGsonStr2List(String str) {
        long a = System.currentTimeMillis();
        Gson gson = new Gson();
        List<String> list = gson.fromJson(str, List.class);
        long b = System.currentTimeMillis();
        System.out.println("GsonStr2List:size=" + list.size() + ",time=" + (b - a));
    }

    public String testGsonList2Str(List<String> list) {
        long a = System.currentTimeMillis();
        Gson gson = new Gson();
        String str = gson.toJson(list);
        long b = System.currentTimeMillis();
        System.out.println("GsonList2Str:length=" + str.length() + ",time=" + (b - a));
        return str;
    }
}

最后编写测试类(Main代替)

package com.love.yu.maven.learn.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.love.yu.maven.learn.enums.CityEnum;
import com.love.yu.maven.learn.util.Enums;

public class CompareJson {
    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 5000000; i++) {
            String name = Enums.random(CityEnum.class).name();
            list.add(name + "-" + i);
        }

        TestJson testJson = new TestJson();
        for (int i = 0; i < 10; i++) {
            String str = testJson.testJsonList2Str(list);
            testJson.testJsonStr2List(str);
        }

        System.out.println("------------");
        TestFastJson fastJson = new TestFastJson();
        for (int i = 0; i < 10; i++) {
            String str = fastJson.testFastJsonList2Str(list);
            fastJson.testFastJsonStr2List(str);
        }

        System.out.println("------------");
        TestJackson testJackson = new TestJackson();
        for (int i = 0; i < 10; i++) {
            String str = testJackson.testJacksonList2Str(list);
            testJackson.testJacksonStr2List(str);
        }
        System.out.println("------------");
        TestGson testGson = new TestGson();
        for (int i = 0; i < 10; i++) {
            String str = testGson.testGsonList2Str(list);
            testGson.testGsonStr2List(str);
        }
    }
}

以size=5000000的list测试转换效率,结果如下

JsonList2Str:length=93890636,time=2060
JsonStr2List:size=5000000,time=4272
JsonList2Str:length=93890636,time=2646
JsonStr2List:size=5000000,time=3270
JsonList2Str:length=93890636,time=2512
JsonStr2List:size=5000000,time=4024
JsonList2Str:length=93890636,time=2366
JsonStr2List:size=5000000,time=3918
JsonList2Str:length=93890636,time=2386
JsonStr2List:size=5000000,time=3977
JsonList2Str:length=93890636,time=2323
JsonStr2List:size=5000000,time=3928
JsonList2Str:length=93890636,time=2352
JsonStr2List:size=5000000,time=4019
JsonList2Str:length=93890636,time=2355
JsonStr2List:size=5000000,time=4001
JsonList2Str:length=93890636,time=2342
JsonStr2List:size=5000000,time=3913
JsonList2Str:length=93890636,time=2401
JsonStr2List:size=5000000,time=4152
------------
FastJsonList2Str:length=93890636,time=2269
FastJsonStr2List:size=5000000,time=747
FastJsonList2Str:length=93890636,time=2150
FastJsonStr2List:size=5000000,time=646
FastJsonList2Str:length=93890636,time=2032
FastJsonStr2List:size=5000000,time=673
FastJsonList2Str:length=93890636,time=2020
FastJsonStr2List:size=5000000,time=652
FastJsonList2Str:length=93890636,time=2149
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2015
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2050
FastJsonStr2List:size=5000000,time=632
FastJsonList2Str:length=93890636,time=1998
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2175
FastJsonStr2List:size=5000000,time=630
FastJsonList2Str:length=93890636,time=2011
FastJsonStr2List:size=5000000,time=624
------------
JacksonList2Str:length=93890636,time=1989
JacksonStr2List:size=5000000,time=671
JacksonList2Str:length=93890636,time=666
JacksonStr2List:size=5000000,time=1763
JacksonList2Str:length=93890636,time=615
JacksonStr2List:size=5000000,time=1843
JacksonList2Str:length=93890636,time=412
JacksonStr2List:size=5000000,time=1643
JacksonList2Str:length=93890636,time=651
JacksonStr2List:size=5000000,time=1727
JacksonList2Str:length=93890636,time=625
JacksonStr2List:size=5000000,time=1755
JacksonList2Str:length=93890636,time=594
JacksonStr2List:size=5000000,time=1584
JacksonList2Str:length=93890636,time=647
JacksonStr2List:size=5000000,time=1704
JacksonList2Str:length=93890636,time=651
JacksonStr2List:size=5000000,time=1817
JacksonList2Str:length=93890636,time=606
JacksonStr2List:size=5000000,time=1626
------------
GsonList2Str:length=93890636,time=2244
GsonStr2List:size=5000000,time=708
GsonList2Str:length=93890636,time=2031
GsonStr2List:size=5000000,time=2133
GsonList2Str:length=93890636,time=2091
GsonStr2List:size=5000000,time=688
GsonList2Str:length=93890636,time=2078
GsonStr2List:size=5000000,time=1721
GsonList2Str:length=93890636,time=2150
GsonStr2List:size=5000000,time=648
GsonList2Str:length=93890636,time=2211
GsonStr2List:size=5000000,time=2679
GsonList2Str:length=93890636,time=1974
GsonStr2List:size=5000000,time=679
GsonList2Str:length=93890636,time=2079
GsonStr2List:size=5000000,time=1706
GsonList2Str:length=93890636,time=2124
GsonStr2List:size=5000000,time=647
GsonList2Str:length=93890636,time=2103
GsonStr2List:size=5000000,time=2604

发现:

  1. str—>list:fastJson在处理数据时速度最快,Gson处理速度也不错,但是并不稳定,Json是最慢的;
  2. list—>str:jackson在处理数据时速度最快,其他三者速度相差不是很大

疑惑:

gson在处理Str—>List的时候,处理时间交替出现快-慢-快-慢的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值