json解析之fastjson和jackson使用对比

前言

最近项目中需要做埋点分析,首先就需要对埋点日志进行解析处理,刚好这时候体验对比了下fastjson和jackson两者使用的区别,以下分别是针对同一个json串处理,最终的效果都是将json数据解析出来,并统一展示。


一、fastjson简介?

fastjson是由国内的阿里推出的一种json处理器,由java语言编写,无依赖,不需要引用额外的jar包,能直接运行在jdk环境中,它的解析速度是非常之快的,目前超过了所有json库。
提示:以下是引用fastjson的方法,数据未涉及到私密信息

java 复制代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;

public class FastJsonTest {
    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\n" +
                "    \"distinct_id\": \"77\",\n" +
                "    \"identities\":\n" +
                "    {\n" +
                "        \"$identity_mp_id\": \"37\",\n" +
                "        \"$identity_login_id\": \"77\",\n" +
                "        \"$identity_mp_wx17a032a586c19379_openid\": \"oiF\",\n" +
                "        \"$identity_anonymous_id\": \"oiF\"\n" +
                "    },\n" +
                "    \"lib\":\n" +
                "    {\n" +
                "        \"$lib\": \"MiniProgram\",\n" +
                "        \"$lib_method\": \"code\",\n" +
                "        \"$lib_version\": \"1.17.7\"\n" +
                "    },\n" +
                "    \"properties\":\n" +
                "    {\n" +
                "        \"$lib\": \"MiniProgram\",\n" +
                "        \"$lib_version\": \"1.17.7\",\n" +
                "        \"$network_type\": \"WIFI\",\n" +
                "        \"$brand\": \"IPHONE\",\n" +
                "        \"$manufacturer\": \"iPhone\",\n" +
                "        \"$model\": \"iPhone 12 Pro Max<iPhone13,4>\",\n" +
                "        \"$screen_width\": 428,\n" +
                "        \"$screen_height\": 926,\n" +
                "        \"$os\": \"iOS\",\n" +
                "        \"$os_version\": \"14.2.1\",\n" +
                "        \"$mp_client_app_version\": \"8.0.44\",\n" +
                "        \"$mp_client_basic_library_version\": \"3.2.5\",\n" +
                "        \"$timezone_offset\": -480,\n" +
                "        \"$app_id\": \"wx17a039\",\n" +
                "        \"$app_version\": \"1.3\",\n" +
                "        \"platform_type\": \"小程序\",\n" +
                "        \"product_name\": \"小程序\",\n" +
                "        \"member_flag\": false,\n" +
                "        \"member_level\": \"非会员\",\n" +
                "        \"$latest_scene\": \"wx-10\",\n" +
                "        \"$scene\": \"wx-10\",\n" +
                "        \"$url_path\": \"pages/Common/Url/index\",\n" +
                "        \"$title\": \"照明\",\n" +
                "        \"$url_query\": \"\",\n" +
                "        \"$referrer\": \"pages/Common/Url/index\",\n" +
                "        \"$referrer_title\": \"照明\",\n" +
                "        \"$url\": \"pages/Common/Url/index\",\n" +
                "        \"$is_first_day\": false,\n" +
                "        \"$ip\": \"11.11.11.11\",\n" +
                "        \"$browser\": \"WeChat\",\n" +
                "        \"$browser_version\": \"8\",\n" +
                "        \"$track_signup_original_id\": \"oiF\",\n" +
                "        \"$is_login_id\": true,\n" +
                "        \"$ad_extra_properties\": \"{\\\"sdf_channel_closed\\\":false}\",\n" +
                "        \"$city\": \"上海\",\n" +
                "        \"$province\": \"上海\",\n" +
                "        \"$country\": \"中国\"\n" +
                "    },\n" +
                "    \"login_id\": \"77\",\n" +
                "    \"anonymous_id\": \"oiF\",\n" +
                "    \"type\": \"track\",\n" +
                "    \"event\": \"$MPShow\",\n" +
                "    \"_track_id\": 5692,\n" +
                "    \"time\": 1703,\n" +
                "    \"_flush_time\": 17032,\n" +
                "    \"device_id\": \"oiFOU\",\n" +
                "    \"project_id\": 2,\n" +
                "    \"map_id\": \"oiFOU\",\n" +
                "    \"user_id\": -4011,\n" +
                "    \"recv_time\": 1703,\n" +
                "    \"extractor\":\n" +
                "    {\n" +
                "        \"f\": \"sdf_input_topic\",\n" +
                "        \"o\": 379,\n" +
                "        \"n\": \"sdf_input_topic\",\n" +
                "        \"s\": 379,\n" +
                "        \"c\": 379,\n" +
                "        \"p\": 1,\n" +
                "        \"e\": \"hyb\"\n" +
                "    },\n" +
                "    \"edge_progress\":\n" +
                "    {\n" +
                "        \"f\": \"(dev=821,ino=537178209)\",\n" +
                "        \"n\": \"access_log.2023122213\",\n" +
                "        \"o\": 1400687,\n" +
                "        \"s\": 37229603,\n" +
                "        \"c\": 37229604,\n" +
                "        \"e\": \"hybr\"\n" +
                "    },\n" +
                "    \"project\": \"test\",\n" +
                "    \"ver\": 2\n" +
                "}";

        JSONObject jsonObject = JSON.parseObject(json);

        new FastJsonTest().travelJSONObject(jsonObject);
    }
    public void travelJSONObject(JSONObject originalJSONObject) {
        for (String key : originalJSONObject.keySet()) {    
            StringBuilder path = new StringBuilder(key);
            Object value = originalJSONObject.get(key);

            if (value instanceof String || value instanceof Number || value instanceof Boolean) {
                System.out.println(key + " : " + value);
                continue;
            }
            if (value instanceof JSONObject) {
                JSONObject object = (JSONObject) value;
                travelJSONObject(object);
            }
        }
    }
}

二、jackson简介

jackson是用来序列化和反序列化json的java开源框架,社区相对比较活跃,更新速度较快,是最流行的json解析器之一,也是Spring MVC默认json解析器。
提示:以下是引用jackson的方法,数据未涉及到私密信息

java 复制代码
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Iterator;

public class JacksonTest {
    public static void main(String[] args) throws JsonProcessingException {
        String json = "{\n" +
                "    \"distinct_id\": \"77\",\n" +
                "    \"identities\":\n" +
                "    {\n" +
                "        \"$identity_mp_id\": \"37\",\n" +
                "        \"$identity_login_id\": \"77\",\n" +
                "        \"$identity_mp_wx17a032a586c19379_openid\": \"oiF\",\n" +
                "        \"$identity_anonymous_id\": \"oiF\"\n" +
                "    },\n" +
                "    \"lib\":\n" +
                "    {\n" +
                "        \"$lib\": \"MiniProgram\",\n" +
                "        \"$lib_method\": \"code\",\n" +
                "        \"$lib_version\": \"1.17.7\"\n" +
                "    },\n" +
                "    \"properties\":\n" +
                "    {\n" +
                "        \"$lib\": \"MiniProgram\",\n" +
                "        \"$lib_version\": \"1.17.7\",\n" +
                "        \"$network_type\": \"WIFI\",\n" +
                "        \"$brand\": \"IPHONE\",\n" +
                "        \"$manufacturer\": \"iPhone\",\n" +
                "        \"$model\": \"iPhone 12 Pro Max<iPhone13,4>\",\n" +
                "        \"$screen_width\": 428,\n" +
                "        \"$screen_height\": 926,\n" +
                "        \"$os\": \"iOS\",\n" +
                "        \"$os_version\": \"14.2.1\",\n" +
                "        \"$mp_client_app_version\": \"8.0.44\",\n" +
                "        \"$mp_client_basic_library_version\": \"3.2.5\",\n" +
                "        \"$timezone_offset\": -480,\n" +
                "        \"$app_id\": \"wx17a039\",\n" +
                "        \"$app_version\": \"1.3\",\n" +
                "        \"platform_type\": \"小程序\",\n" +
                "        \"product_name\": \"小程序\",\n" +
                "        \"member_flag\": false,\n" +
                "        \"member_level\": \"非会员\",\n" +
                "        \"$latest_scene\": \"wx-10\",\n" +
                "        \"$scene\": \"wx-10\",\n" +
                "        \"$url_path\": \"pages/Common/Url/index\",\n" +
                "        \"$title\": \"照明\",\n" +
                "        \"$url_query\": \"\",\n" +
                "        \"$referrer\": \"pages/Common/Url/index\",\n" +
                "        \"$referrer_title\": \"照明\",\n" +
                "        \"$url\": \"pages/Common/Url/index\",\n" +
                "        \"$is_first_day\": false,\n" +
                "        \"$ip\": \"11.11.11.11\",\n" +
                "        \"$browser\": \"WeChat\",\n" +
                "        \"$browser_version\": \"8\",\n" +
                "        \"$track_signup_original_id\": \"oiF\",\n" +
                "        \"$is_login_id\": true,\n" +
                "        \"$ad_extra_properties\": \"{\\\"sdf_channel_closed\\\":false}\",\n" +
                "        \"$city\": \"上海\",\n" +
                "        \"$province\": \"上海\",\n" +
                "        \"$country\": \"中国\"\n" +
                "    },\n" +
                "    \"login_id\": \"77\",\n" +
                "    \"anonymous_id\": \"oiF\",\n" +
                "    \"type\": \"track\",\n" +
                "    \"event\": \"$MPShow\",\n" +
                "    \"_track_id\": 5692,\n" +
                "    \"time\": 1703,\n" +
                "    \"_flush_time\": 17032,\n" +
                "    \"device_id\": \"oiFOU\",\n" +
                "    \"project_id\": 2,\n" +
                "    \"map_id\": \"oiFOU\",\n" +
                "    \"user_id\": -4011,\n" +
                "    \"recv_time\": 1703,\n" +
                "    \"extractor\":\n" +
                "    {\n" +
                "        \"f\": \"sdf_input_topic\",\n" +
                "        \"o\": 379,\n" +
                "        \"n\": \"sdf_input_topic\",\n" +
                "        \"s\": 379,\n" +
                "        \"c\": 379,\n" +
                "        \"p\": 1,\n" +
                "        \"e\": \"hyb\"\n" +
                "    },\n" +
                "    \"edge_progress\":\n" +
                "    {\n" +
                "        \"f\": \"(dev=821,ino=537178209)\",\n" +
                "        \"n\": \"access_log.2023122213\",\n" +
                "        \"o\": 1400687,\n" +
                "        \"s\": 37229603,\n" +
                "        \"c\": 37229604,\n" +
                "        \"e\": \"hybr\"\n" +
                "    },\n" +
                "    \"project\": \"test\",\n" +
                "    \"ver\": 2\n" +
                "}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(json);

        Iterator<String> keys = jsonNode.fieldNames();

        while (keys.hasNext()){
            String key = keys.next();
            JsonNode value = jsonNode.get(key);
            if(value instanceof JsonNode){
                Iterator<String> subkeys = value.fieldNames();
                while (subkeys.hasNext()){
                    String subkey = subkeys.next();
                    JsonNode subvalue = value.get(subkey);
                    System.out.println(subkey + " : " + subvalue);
                }
            }else{
                System.out.println(key + ":"+ "\t" + jsonNode.get(key)) ;
            }
        }
    }
}

总结

分别对两个json解析器进行试用,个人感觉jackson代码比较优雅,类似java jdbc连接数据的使用规范,估计这也是国外特别喜欢用它的原因吧,而fastjson的感觉就是速度特别快,用起来也比较简单,注重实用性,当然了,上面只是一个简单的解析代码,并没有涉及到复杂的操作,由于时间关系,只是简单介绍下两者的使用方法,关于两者的使用,后续会结合项目中的场景持续更新。

相关推荐
武子康1 小时前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
lucky_syq3 小时前
Flume和Kafka的区别?
大数据·kafka·flume
AI_NEW_COME3 小时前
构建全方位大健康零售帮助中心:提升服务与体验
大数据·人工智能
it噩梦4 小时前
es 中 terms set 使用
大数据·elasticsearch
中科岩创4 小时前
中科岩创边坡自动化监测解决方案
大数据·网络·物联网
DolphinScheduler社区5 小时前
作业帮基于 Apache DolphinScheduler 3_0_0 的缺陷修复与优化
大数据
SeaTunnel5 小时前
京东科技基于 Apache SeaTunnel 复杂场景适配 #数据集成
大数据
喝醉酒的小白6 小时前
Elasticsearch 配置文件
大数据·elasticsearch·搜索引擎
一只敲代码的猪6 小时前
Llama 3 模型系列解析(一)
大数据·python·llama
智慧化智能化数字化方案7 小时前
深入解读数据资产化实践指南(2024年)
大数据·人工智能·数据资产管理·数据资产入表·数据资产化实践指南