转载参考:
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 Listdata; 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