Gson解析json数据
json解析我们不陌生了,不过每次编写解析的代码时总是很头疼,一堆很无聊的代码,如果有50个接口那就得写50个相似的解析处理。其实谷歌和阿里很早就推出了解析json的工具包,谷歌的Gson,阿里的FastJson,还有一个jackJson。有人说jackJson解析速度快,大数据时FastJson要比Gson效率高,小数据时反之。不过我还是偏爱谷歌的Gson,即使效率可能偏低,但是稳定性肯定没得说,FastJson和jackJson就不知道了。
现在我们来看看Json是如何解析的:
1.下载Gson开发包,放入工程libs目录下:
http://download.csdn.net/detail/itachi85/8617065
2.编写Bean文件:
先看一下json数据:
{ "array": [ { "username": "在中", "recommend": 0, "user_photo": "0", "like": false, "reply_cnt": 3, "distance": "2154m", "add_date": "3天前", "text": "肯德基快吃吐了,明天去麦当劳看看", "stamp__name": null, "place_id": 1, "ptype": "d", "like_cnt": "2", "posting_image_url": "1", "stamp_name": null, "user": 0, "stamp__id": null, "dislike": "0", "icon": "cate_icon_a_09", "id": 0, "mayor_cnt": 2, "place_name": "东大桥肯德基", "place_level": 2 }, { "username": "在中", "recommend": 0, "user_photo": "0", "like": false, "reply_cnt": 0, "distance": "2425m", "add_date": "3天前", "text": "这是在跳僵尸舞呢吗?", "stamp__name": null, "place_id": 7, "ptype": "d", "like_cnt": "0", "posting_image_url": "0", "stamp_name": null, "user": 0, "stamp__id": null, "dislike": "0", "icon": "cate_icon_b_05", "id": 4, "mayor_cnt": 2, "place_name": "悠唐", "place_level": 1 } ], "error": 0, "next": true }
紧接着,我们创建bean文件
需要注意的是:
1:内部嵌套的类必须是static修饰的;
2:类里面的属性名必须跟Json字段里面的key是一样的。
NearDynamic.java:
1 public class NearDynamic { 2 private Boolean next=false; 3 private int error; 4 private ArrayList<Dynamic> array = new ArrayList<Dynamic>(); 5 public Boolean getNext() { 6 return next; 7 } 8 public void setNext(Boolean next) { 9 this.next = next; 10 } 11 public int getError() { 12 return error; 13 } 14 public void setError(int error) { 15 this.error = error; 16 } 17 public ArrayList<DynamicJ> getArray() { 18 return array; 19 } 20 public void setArray(ArrayList<DynamicJ> array) { 21 this.array = array; 22 } 23 }
上面包含了error,next字段,以及包含类型是Dynamic的List,接着我们创建Dynamic:
1 public class Dynamic { 2 private String username; 3 private int recommend; 4 private String user_photo; 5 private Boolean like; 6 private int replycnt; 7 private String distance; 8 private String add_date; 9 private String text; 10 private String stamp__name; 11 private int place_id; 12 private int like_cnt; 13 private String ptype; 14 private String posting_image_url; 15 private String stamp_name; 16 private int user; 17 private String stamp__id; 18 private int dislike=0; 19 private String icon; 20 private int id; 21 private String place_name; 22 private int place_level; 23 private String mayor_cnt; 24 public String getStamp__name() { 25 return stamp__name; 26 } 27 public void setStamp__name(String stamp__name) { 28 this.stamp__name = stamp__name; 29 } 30 public int getLike_cnt() { 31 return like_cnt; 32 } 33 public void setLike_cnt(int like_cnt) { 34 this.like_cnt = like_cnt; 35 } 36 public String getStamp_name() { 37 return stamp_name; 38 } 39 public void setStamp_name(String stamp_name) { 40 this.stamp_name = stamp_name; 41 } 42 public String getStamp__id() { 43 return stamp__id; 44 } 45 public void setStamp__id(String stamp__id) { 46 this.stamp__id = stamp__id; 47 } 48 public int getDislike() { 49 return dislike; 50 } 51 public void setDislike(int dislike) { 52 this.dislike = dislike; 53 } 54 public String getMayor_cnt() { 55 return mayor_cnt; 56 } 57 public void setMayor_cnt(String mayor_cnt) { 58 this.mayor_cnt = mayor_cnt; 59 } 60 public int getId() { 61 return id; 62 } 63 public void setId(int id) { 64 this.id = id; 65 } 66 public int getUser() { 67 return user; 68 } 69 public void setUser(int user) { 70 this.user = user; 71 } 72 public String getUsername() { 73 return username; 74 } 75 ... 76 }
3.使用Gson解析数据:
1 private int parseUserData() { 2 String strContent = Utils.getJson("neardynamic.txt"); 3 if (!TextUtils.isEmpty(strContent)) { 4 try { 5 Gson mgson = new Gson(); 6 NearDynamic mNearDynamic = mgson.fromJson( 7 strContent, NearDynamic.class); 8 ArrayList<Dynamic> mNearDynamicList = mNearDynamic 9 .getArray(); 10 LogController.i(mNearDynamicList.get(1).getUsername()); 11 return BaseEngine.RESULT_OK; 12 } catch (Exception e) { 13 e.printStackTrace(); 14 return BaseEngine.PARSEJSON_ERR; 15 } 16 } 17 return BaseEngine.RESULT_ERR; 18 }
上面代码是一个解析的方法:直接从txt文件中读取数据由Json解析,首先由Json生成NearDynamic,然后得到array中的列表数据,我们取第二个数据打下log,得出的结果是"在中",解析完毕。
如果想要快速测试的话,就写一个txt文件,里面写有json数据,放入assets文件中,然后用下面的方法读取txt文件中数据,再按照上面所讲的就可以了:
1 public static String getJson(String filename) { 2 InputStream mInputStream = null; 3 String resultString = ""; 4 try { 5 mInputStream = App.mContext.getAssets().open(filename); 6 byte[] buffer = new byte[mInputStream.available()]; 7 mInputStream.read(buffer); 8 resultString = new String(buffer, "GB2312"); 9 } catch (IOException e) { 10 // TODO Auto-generated catch block 11 e.printStackTrace(); 12 } finally { 13 try { 14 mInputStream.close(); 15 } catch (IOException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 } 20 return resultString.toString(); 21 }
想要了解Gson更多的方法可以下载http://download.csdn.net/detail/itachi85/8617065中的文件,里面有Gson的文档。