Android解析JSON速度对比

时间:2023-03-08 21:59:40

转载参考:http://blog.****.net/h3c4lenovo/article/details/26568531

 {
"testStr":"这是String的测试",
"testInt":12443,
"data": [
{
"children": [
{
"id": 10007,
"title": "北京",
"type": 1,
"url": "/10007/list_1.json"
},
{
"id": 10105,
"title": "汽车",
"type": 1,
"url": "/10105/list_1.json"
}
],
"id": 10000,
"title": "新闻",
"type": 1
},
{
"id": 10002,
"title": "专题",
"type": 10,
"url": "/10006/list_1.json",
"url1": "/10007/list1_1.json"
},
{
"id": 10003,
"title": "组图",
"type": 2,
"url": "/10008/list_1.json"
},
{
"dayurl": "",
"excurl": "",
"id": 10004,
"title": "互动",
"type": 3,
"weekurl": ""
}
],
"extend": [
10007,
10006,
10008,
10014,
10012,
10091,
10009,
10010,
10095
],
"retcode": 200
}

     以下测试以上面这段JSON作为参考进行测试。

第三方JAR包:(阿里巴巴)fastjson-1.2.5.jar      (谷歌)gson-2.2.4.jar

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class MainActivity extends Activity {
public final String TAG = "MainActivity"; Gson my_gson = new Gson();
java.lang.reflect.Type my_type = new TypeToken<TestBean1>() {
}.getType(); @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.i(TAG, "start...");
try {
long startTime2 = System.currentTimeMillis();
String json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");
for(int n = 0;n < 100000; n++) {
TestBean1 toBean = JSON.parseObject(json, TestBean1.class);
System.out.println(toBean);
}
long endTime2 = System.currentTimeMillis() - startTime2;
Log.i(TAG, "fastjson....." + endTime2); long startTime4 = System.currentTimeMillis();
String my_json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");
for (int n = 0; n < 100000; n++) {
// 使用JSON 操作 工具将JSON字符串封装到实体类
TestBean1 toBean = my_gson.fromJson(my_json, my_type);
System.out.println(toBean);
}
long endTime4 = System.currentTimeMillis() - startTime4;
Log.i(TAG, "gson....." + endTime4); } catch (Exception e) {
e.printStackTrace();
}
} }
public class TestBean1 {
public String testStr;
public Integer testInt; public List<NewsItem> data;
public List<Integer> extend;
public int retcode; public class NewsItem {
public List<NewsCategory> children;
public int id;
public String title;
public int type;
public String url;
public String url1;
public String dayurl;
public String excurl;
public String weekurl; public class NewsCategory {
public int id;
public String title;
public int type;
public String url;
}
} }

运行结果:

gson.....122298
fastjson.....138429