博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android解析JSON速度对比
阅读量:7061 次
发布时间:2019-06-28

本文共 4044 字,大约阅读时间需要 13 分钟。

转载参考:

 

1 { 2     "testStr":"这是String的测试", 3     "testInt":12443, 4     "data": [ 5         { 6             "children": [ 7                 { 8                     "id": 10007, 9                     "title": "北京",10                     "type": 1,11                     "url": "/10007/list_1.json"12                 },13                 {14                     "id": 10105,15                     "title": "汽车",16                     "type": 1,17                     "url": "/10105/list_1.json"18                 }19             ],20             "id": 10000,21             "title": "新闻",22             "type": 123         },24         {25             "id": 10002,26             "title": "专题",27             "type": 10,28             "url": "/10006/list_1.json",29             "url1": "/10007/list1_1.json"30         },31         {32             "id": 10003,33             "title": "组图",34             "type": 2,35             "url": "/10008/list_1.json"36         },37         {38             "dayurl": "",39             "excurl": "",40             "id": 10004,41             "title": "互动",42             "type": 3,43             "weekurl": ""44         }45     ],46     "extend": [47         10007,48         10006,49         10008,50         10014,51         10012,52         10091,53         10009,54         10010,55         1009556     ],57     "retcode": 20058 }

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

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

1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.util.Log; 4 import com.alibaba.fastjson.JSON; 5 import com.google.gson.Gson; 6 import com.google.gson.reflect.TypeToken; 7  9 public class MainActivity extends Activity {10     public final String TAG = "MainActivity";11 12     Gson my_gson = new Gson();13     java.lang.reflect.Type my_type = new TypeToken
() {14 }.getType();15 16 @Override17 protected void onCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.activity_main);20 21 new Thread(new Runnable() {22 @Override23 public void run() {24 doSth();25 }26 }).start();27 }28 29 private void doSth() {30 Log.i(TAG, "start...");31 try {32 long startTime2 = System.currentTimeMillis();33 String json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");34 for(int n = 0;n < 100000; n++) {35 TestBean1 toBean = JSON.parseObject(json, TestBean1.class);36 System.out.println(toBean);37 }38 long endTime2 = System.currentTimeMillis() - startTime2;39 Log.i(TAG, "fastjson....." + endTime2);40 41 42 long startTime4 = System.currentTimeMillis();43 String my_json = LocalFileUtils.getStringFormAsset(this, "testbean1.json");44 for (int n = 0; n < 100000; n++) {45 // 使用JSON 操作 工具将JSON字符串封装到实体类46 TestBean1 toBean = my_gson.fromJson(my_json, my_type);47 System.out.println(toBean);48 }49 long endTime4 = System.currentTimeMillis() - startTime4;50 Log.i(TAG, "gson....." + endTime4);51 52 } catch (Exception e) {53 e.printStackTrace();54 }55 }56 57 }

 

public class TestBean1 {    public String testStr;    public Integer testInt;    public List
data; public List
extend; public int retcode; public class NewsItem { public List
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

 

你可能感兴趣的文章
动画在webapp中的现状
查看>>
权限框架 - shiro 自定义realm
查看>>
基于MVC4+EasyUI的Web开发框架经验总结(16)--使用云打印控件C-Lodop打印页面或套打报关运单信息...
查看>>
MySQL 语句使用到的关键字 函数 记录
查看>>
5.5. VoIP / Meeting
查看>>
[LeetCode] Valid Palindrome
查看>>
聊下 git remote prune origin
查看>>
【转】如何用 Chrome for Android 做远程遥控 debugging
查看>>
Ajax异步验证登陆或者注册
查看>>
整合百度推送碰到的问题
查看>>
使用Fusioncharts实现后台处理进度的前台展示
查看>>
CentOS 7下配置本地yum源及yum客户端
查看>>
Mybatis中的collection、association来处理结果映射
查看>>
如何对 GIT 分支进行规划? (转)
查看>>
浅谈简单工作流设计——责任链模式配合策略与命令模式的实现
查看>>
HDOJ(HDU) 1406 完数
查看>>
gradle项目中资源文件的相对路径打包处理技巧
查看>>
让手机支持OTG,不看绝对后悔! - 我也做一回搬运工,解决RFID读卡器OTG支持问题...
查看>>
Hadoop-2.7.0 HDFS DataXceiverServer两个参数的疑问
查看>>
linux exec函数家族
查看>>