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的感觉就是速度特别快,用起来也比较简单,注重实用性,当然了,上面只是一个简单的解析代码,并没有涉及到复杂的操作,由于时间关系,只是简单介绍下两者的使用方法,关于两者的使用,后续会结合项目中的场景持续更新。

相关推荐
Elastic 中国社区官方博客42 分钟前
如何将数据从 AWS S3 导入到 Elastic Cloud - 第 3 部分:Elastic S3 连接器
大数据·elasticsearch·搜索引擎·云计算·全文检索·可用性测试·aws
Aloudata2 小时前
从Apache Atlas到Aloudata BIG,数据血缘解析有何改变?
大数据·apache·数据血缘·主动元数据·数据链路
水豚AI课代表2 小时前
分析报告、调研报告、工作方案等的提示词
大数据·人工智能·学习·chatgpt·aigc
努力的家伙是不讨厌的3 小时前
解析json导出csv或者直接入库
开发语言·python·json
拓端研究室TRL5 小时前
【梯度提升专题】XGBoost、Adaboost、CatBoost预测合集:抗乳腺癌药物优化、信贷风控、比特币应用|附数据代码...
大数据
黄焖鸡能干四碗5 小时前
信息化运维方案,实施方案,开发方案,信息中心安全运维资料(软件资料word)
大数据·人工智能·软件需求·设计规范·规格说明书
编码小袁5 小时前
探索数据科学与大数据技术专业本科生的广阔就业前景
大数据
WeeJot嵌入式6 小时前
大数据治理:确保数据的可持续性和价值
大数据
zmd-zk7 小时前
kafka+zookeeper的搭建
大数据·分布式·zookeeper·中间件·kafka
激流丶7 小时前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic