Scenario Description:
This articleOnly verifies the parsing speed of using Json Key as the deserialization condition in Android Environment. The conclusion is that the fastest resolution speed is not Alibaba's fastjson, nor Google's Gson, but json-smart.
Android 4.4.2
Fastjson-1.1.34.android.jar
Gson-2.2.4.jar
Json-smart-2.0-RC3.jar
** Note the limitations of the scenario **
Core code:
package com.h3c.mytestview;import java.io.StringReader;import net.minidev.json.JSONValue;import android.app.Activity;import android.os.Bundle;import android.util.Log;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.google.gson.Gson;import com.google.gson.JsonElement;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import com.google.gson.stream.JsonReader;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { doSth(); } }).start(); } private void doSth() { Log.e("H6c","start..."); String json = "{\"name\":\"reiz\"}"; String k = ""; try { long startTime1 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { org.json.JSONObject jo; jo = new org.json.JSONObject(json); k = jo.getString("name"); } long endTime1 = System.currentTimeMillis() - startTime1; Log.e("H6c","android:"+ endTime1); long startTime2 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { JSONObject jo = JSON.parseObject(json); k = jo.getString("name"); } long endTime2 = System.currentTimeMillis() - startTime2; Log.e("H6c","fastjson:"+ endTime2); long startTime3 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { net.minidev.json.JSONObject jo = (net.minidev.json.JSONObject)JSONValue.parseStrict(json); k = (String)jo.get("name"); } long endTime3 = System.currentTimeMillis() - startTime3; Log.e("H6c","json-smart:"+endTime3); long startTime4 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { JsonElement je = new JsonParser().parse(json); JsonObject jo = je.getAsJsonObject(); k = jo.get("name").getAsString(); } long endTime4 = System.currentTimeMillis() - startTime4; Log.e("H6c","gson:"+endTime4); } catch (Exception e) { e.printStackTrace(); } }}
Test results:
Galaxy S3
===========Galaxy S3=============105-22 11:04:17.120: E/H6c(28681): start...05-22 11:04:21.525: E/H6c(28681): android:440005-22 11:04:28.135: E/H6c(28681): fastjson:660805-22 11:04:31.600: E/H6c(28681): json-smart:346705-22 11:04:47.440: E/H6c(28681): gson:15839===========Galaxy S3=============205-22 11:05:08.230: E/H6c(28681): start...05-22 11:05:12.120: E/H6c(28681): android:388605-22 11:05:17.285: E/H6c(28681): fastjson:516705-22 11:05:21.020: E/H6c(28681): json-smart:373605-22 11:05:34.040: E/H6c(28681): gson:13018===========Galaxy S3=============305-22 11:05:45.440: E/H6c(28681): start...05-22 11:05:49.470: E/H6c(28681): android:403305-22 11:05:54.500: E/H6c(28681): fastjson:503105-22 11:05:58.240: E/H6c(28681): json-smart:373605-22 11:06:13.485: E/H6c(28681): gson:15245
Galaxy S4
===========Galaxy S4=============105-22 10:58:03.541: E/H6c(26600): start...05-22 10:58:06.934: E/H6c(26600): android:339305-22 10:58:12.680: E/H6c(26600): fastjson:575205-22 10:58:15.232: E/H6c(26600): json-smart:254905-22 10:58:24.841: E/H6c(26600): gson:9610===========Galaxy S4=============205-22 11:00:51.675: E/H6c(26600): start...05-22 11:00:54.878: E/H6c(26600): android:320905-22 11:01:00.413: E/H6c(26600): fastjson:552605-22 11:01:02.986: E/H6c(26600): json-smart:256905-22 11:01:11.084: E/H6c(26600): gson:8099===========Galaxy S4=============305-22 11:01:31.213: E/H6c(26600): start...05-22 11:01:34.717: E/H6c(26600): android:350805-22 11:01:40.172: E/H6c(26600): fastjson:544305-22 11:01:42.734: E/H6c(26600): json-smart:256605-22 11:01:52.554: E/H6c(26600): gson:9823
Xiaomi3
===========Xiaomi 3=============105-22 11:02:05.461: E/H6c(30473): start...05-22 11:02:08.021: E/H6c(30473): android:256105-22 11:02:11.781: E/H6c(30473): fastjson:376605-22 11:02:13.411: E/H6c(30473): json-smart:162905-22 11:02:16.811: E/H6c(30473): gson:3402===========Xiaomi 3=============205-22 11:02:30.291: E/H6c(30473): start...05-22 11:02:32.481: E/H6c(30473): android:218405-22 11:02:36.341: E/H6c(30473): fastjson:385605-22 11:02:37.781: E/H6c(30473): json-smart:143905-22 11:02:40.991: E/H6c(30473): gson:3210===========Xiaomi 3=============305-22 11:02:47.731: E/H6c(30473): start...05-22 11:02:50.271: E/H6c(30473): android:254805-22 11:02:53.831: E/H6c(30473): fastjson:355305-22 11:02:55.261: E/H6c(30473): json-smart:143105-22 11:02:58.571: E/H6c(30473): gson:3310
From the test results:
In the scenario where key is used to parse json, the fastest speed is json-smart, which improves the resolution speed.
The conclusion of this study is that Android native Java parsing already has basic json parsing capabilities and has certain speed advantages. In simple scenarios, you do not need to reference fastjson and gson, which are relatively large. In addition, json-smart does not need to be introduced in scenarios where there is no extreme pursuit of speed, because it may only help you save 1 s of time in the background, but is not visually noticeable.