筆者在Android開發中都是用的’Gson’進行的Json數據解析,但都說’FastJson’解析Json數據要比’Gson’快,筆者剛開始也是這麼認爲,至少它的名字中有’Fast’,所以筆者就在這裏專門做了測試兩者的解析性能
Gson版本:2.3.1
FastJson版本:1.1.43 android
項目 版本 GitHub Gson 2.3.1 https://github.com/google/gson FastJson 1.1.43 android https://github.com/alibaba/fastjson/wiki/Android%E7%89%88%E6%9C%AC
Test Jave Bean
//**中國區域級別Json數據JavaBean*/
public class AreasBean {
private String level;
private String parent_id;
private String name;
private String postcode;
private String id;
private List<AreasBean> subarea;
...get set...
}
Test Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--解析Json字符串至對象-->
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:textAllCaps="false"
android:id="@+id/btn_testGsonParseJsonToObject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testGsonParseJsonToObject" />
<Button
android:textAllCaps="false"
android:id="@+id/btn_testFastJsonParseJsonToObject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testFastJsonParseJsonToObject" />
</LinearLayout>
/<!--解析對象至Json字符串-->
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:textAllCaps="false"
android:id="@+id/btn_testGsonParseObjectToJson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testGsonParseObjectToJson" />
<Button
android:textAllCaps="false"
android:id="@+id/btn_testFastJsonParseObjectToJson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testFastJsonParseObjectToJson" />
</LinearLayout>
</LinearLayout>
Test Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static String areasJson = "";
private static List<AreasBean> areasBeans1;
private static List<AreasBean> areasBeans2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
areasJson = getString(R.string.json);
findViewById(R.id.btn_testGsonParseJsonToObject).setOnClickListener(this);
findViewById(R.id.btn_testFastJsonParseJsonToObject).setOnClickListener(this);
findViewById(R.id.btn_testGsonParseObjectToJson).setOnClickListener(this);
findViewById(R.id.btn_testFastJsonParseObjectToJson).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
/**測試Gson解析Json成對象*/
case R.id.btn_testGsonParseJsonToObject:
testGsonParseJsonToObject();
break;
/**測試FastJson解析Json成對象*/
case R.id.btn_testFastJsonParseJsonToObject:
testFastJsonParseJsonToObject();
break;
/**測試Gson解析對象成Json*/
case R.id.btn_testGsonParseObjectToJson:
testGsonParseObjectToJson();
break;
/**測試FastJson解析對象成Json*/
case R.id.btn_testFastJsonParseObjectToJson:
testFastJsonParseObjectToJson();
break;
}
}
//**
* 測試用 Gson 解析 Json 數據成對象
*/
private static void testGsonParseJsonToObject() {
final long time0 = System.currentTimeMillis();
Gson gson = new Gson();
final Type type = new TypeToken<List<AreasBean>>() {}.getType();
for (int i = 0; i < 10; i++) {
areasBeans1 = gson.fromJson(areasJson, type);
}
Log.d("Test Json", "testGsonParseJsonToObject time = " + (System.currentTimeMillis() - time0));
}
//**
* 測試用 FastJson 解析 Json 數據成對象
*/
private static void testFastJsonParseJsonToObject() {
final long time0 = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
areasBeans2 = JSON.parseArray(areasJson, AreasBean.class);
}
Log.d("Test Json", "testFastJsonParseJsonToObject time = " + (System.currentTimeMillis() - time0));
}
//**
* 測試用 Gson 將對象解析 Json 數據
*/
private static void testGsonParseObjectToJson() {
final long time0 = System.currentTimeMillis();
String jsonString = new Gson().toJson(areasBeans1);
Log.d("Test Json", "testGsonParseObjectToJson time = " + (System.currentTimeMillis() - time0));
}
//**
* 測試用 FastJson 將對象解析 Json 數據
*/
private static void testFastJsonParseObjectToJson(){
final long time0 = System.currentTimeMillis();
String jsonString = JSON.toJSONString(areasBeans2);
Log.d("Test Json", "testFastJsonParseObjectToJson time = " + (System.currentTimeMillis() - time0));
}
}
Test Result Log
Gson與FastJson分別解析Json到JaveBean所用時間對比
D/Test Json﹕ Gson:testGsonParseJsonToObect time = 222
D/Test Json﹕ FastJson:testFastJsonParseJsonToObect time = 283
D/Test Json﹕ Gson:testGsonParseJsonToObect time = 206
D/Test Json﹕ FastJson:testFastJsonParseJsonToObect time = 328
D/Test Json﹕ Gson:testGsonParseJsonToObect time = 207
D/Test Json﹕ FastJson:testFastJsonParseJsonToObect time = 295
結論:Gson解析比FastJson快Gson與FastJson分別循環10次解析Json到JaveBean所用時間對比
D/Test Json﹕ Gson:testGsonParseJsonToObject time = 2141
D/Test Json﹕ Gson:testGsonParseJsonToObject time = 2117
D/Test Json﹕ FastJson:testFastJsonParseJsonToObject time = 2841
D/Test Json﹕ FastJson:testFastJsonParseJsonToObject time = 2740
結論:Gson解析比FastJson快Gson與FastJson分別解析JaveBean到Json所用時間對比
D/Test Json﹕ testGsonParseObjectToJson time = 306
D/Test Json﹕ testFastJsonParseObjectToJson time = 231
D/Test Json﹕ testGsonParseObjectToJson time = 207
D/Test Json﹕ testFastJsonParseObjectToJson time = 227
D/Test Json﹕ testGsonParseObjectToJson time = 200
D/Test Json﹕ testFastJsonParseObjectToJson time = 237
D/Test Json﹕ testGsonParseObjectToJson time = 194
D/Test Json﹕ testFastJsonParseObjectToJson time = 238
D/Test Json﹕ testGsonParseObjectToJson time = 196
D/Test Json﹕ testFastJsonParseObjectToJson time = 229
D/Test Json﹕ testGsonParseObjectToJson time = 189
D/Test Json﹕ testFastJsonParseObjectToJson time = 228
D/Test Json﹕ testGsonParseObjectToJson time = 189
D/Test Json﹕ testFastJsonParseObjectToJson time = 228
D/Test Json﹕ testGsonParseObjectToJson time = 190
D/Test Json﹕ testFastJsonParseObjectToJson time = 236
結論:第一次調用方法FastJson比Gson解析快,但接着調用方法解析,Gson每次都要比FastJson快Gson與FastJson分別循環10次解析JaveBean到Json所用時間對比
D/Test Json﹕ Gson:testGsonParseObectToJson time = 2138
D/Test Json﹕ FastJson:testFastJsonParseObectToJson time = 2241
D/Test Json﹕ Gson:testGsonParseObectToJson time = 2924
D/Test Json﹕ FastJson:testFastJsonParseObectToJson time = 3246
結論:Gson比FastJson快
結論
經過上面的簡單測試,FastJson並沒有說的那麼Fast,不過可能筆者單方面測試可能結果並不準確,但這在我們開發中的代碼並無不同,所以說,在開發能滿足我們需求的而速度快的還是Google的Gson解析要快《個人見解,如果測試方面有什麼問題或意見,歡迎大家留言一起討論》