前言
最近项目中需要做埋点分析,首先就需要对埋点日志进行解析处理,刚好这时候体验对比了下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的感觉就是速度特别快,用起来也比较简单,注重实用性,当然了,上面只是一个简单的解析代码,并没有涉及到复杂的操作,由于时间关系,只是简单介绍下两者的使用方法,关于两者的使用,后续会结合项目中的场景持续更新。