JSON

Android解析Json速度最快的库:json

字号+ 作者:H5之家 来源:H5之家 2016-02-13 18:00 我要评论( )

Android解析Json速度最快的库:json-smart,安卓

场景描写叙述:

    本文仅验证了在安卓环境下使用Json的Key作为反序列化条件的解析速度。结论是解析速度最快的不是阿里的fastjson,也不是Google的Gson,而是json-smart。

    Android 4.4.2

    fastjson-1.1.34.android.jar

    gson-2.2.4.jar

    json-smart-2.0-RC3.jar

    **注意场景的限定条件**

核心代码:

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(); } } }

測试结果:

Galaxy S3

===========Galaxy S3=============1 05-22 11:04:17.120: E/H6c(28681): start... 05-22 11:04:21.525: E/H6c(28681): android:4400 05-22 11:04:28.135: E/H6c(28681): fastjson:6608 05-22 11:04:31.600: E/H6c(28681): json-smart:3467 05-22 11:04:47.440: E/H6c(28681): gson:15839 ===========Galaxy S3=============2 05-22 11:05:08.230: E/H6c(28681): start... 05-22 11:05:12.120: E/H6c(28681): android:3886 05-22 11:05:17.285: E/H6c(28681): fastjson:5167 05-22 11:05:21.020: E/H6c(28681): json-smart:3736 05-22 11:05:34.040: E/H6c(28681): gson:13018 ===========Galaxy S3=============3 05-22 11:05:45.440: E/H6c(28681): start... 05-22 11:05:49.470: E/H6c(28681): android:4033 05-22 11:05:54.500: E/H6c(28681): fastjson:5031 05-22 11:05:58.240: E/H6c(28681): json-smart:3736 05-22 11:06:13.485: E/H6c(28681): gson:15245
Galaxy S4

===========Galaxy S4=============1 05-22 10:58:03.541: E/H6c(26600): start... 05-22 10:58:06.934: E/H6c(26600): android:3393 05-22 10:58:12.680: E/H6c(26600): fastjson:5752 05-22 10:58:15.232: E/H6c(26600): json-smart:2549 05-22 10:58:24.841: E/H6c(26600): gson:9610 ===========Galaxy S4=============2 05-22 11:00:51.675: E/H6c(26600): start... 05-22 11:00:54.878: E/H6c(26600): android:3209 05-22 11:01:00.413: E/H6c(26600): fastjson:5526 05-22 11:01:02.986: E/H6c(26600): json-smart:2569 05-22 11:01:11.084: E/H6c(26600): gson:8099 ===========Galaxy S4=============3 05-22 11:01:31.213: E/H6c(26600): start... 05-22 11:01:34.717: E/H6c(26600): android:3508 05-22 11:01:40.172: E/H6c(26600): fastjson:5443 05-22 11:01:42.734: E/H6c(26600): json-smart:2566 05-22 11:01:52.554: E/H6c(26600): gson:9823
Xiaomi3

===========Xiaomi 3=============1 05-22 11:02:05.461: E/H6c(30473): start... 05-22 11:02:08.021: E/H6c(30473): android:2561 05-22 11:02:11.781: E/H6c(30473): fastjson:3766 05-22 11:02:13.411: E/H6c(30473): json-smart:1629 05-22 11:02:16.811: E/H6c(30473): gson:3402 ===========Xiaomi 3=============2 05-22 11:02:30.291: E/H6c(30473): start... 05-22 11:02:32.481: E/H6c(30473): android:2184 05-22 11:02:36.341: E/H6c(30473): fastjson:3856 05-22 11:02:37.781: E/H6c(30473): json-smart:1439 05-22 11:02:40.991: E/H6c(30473): gson:3210 ===========Xiaomi 3=============3 05-22 11:02:47.731: E/H6c(30473): start... 05-22 11:02:50.271: E/H6c(30473): android:2548 05-22 11:02:53.831: E/H6c(30473): fastjson:3553 05-22 11:02:55.261: E/H6c(30473): json-smart:1431 05-22 11:02:58.571: E/H6c(30473): gson:3310
从測试结果来看:

    在使用key解析json的场景下,速度最快的是json-smart,其解析速度有所提升。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评