Json parsing and Gson parsing, Json parsing Gson Parsing

Source: Internet
Author: User

Json parsing and Gson parsing, Json parsing Gson Parsing

This article mainly introduces the original parsing of json and the gson tool class parsing provided by google.

① Json Parsing

1/** 2 * Common json parsing 3 * @ param s 4 * @ throws JSONException 5 */6 private void jsonJieXi (String s) throws JSONException {7 // create a json object 8 JSONObject jsonObject1 = new JSONObject (s); 9 String retcode = jsonObject1.getString ("retcode "); 10 String header = jsonObject1.getString ("header"); 11 Log. I (TAG, "retcode =" + retcode + "---------- header =" + header); 12 13 JSONArray data = jsonObject1.getJSONArray ("data "); 14 15 for (int I = 0; I <data. length (); I ++) {16 JSONObject obj = (JSONObject) data. get (I); 17 String ids = (String) obj. get ("id"); 18 String title = (String) obj. get ("title"); 19 String type = (String) obj. get ("type"); 20 String des = (String) obj. get ("des"); 21 Log. I (TAG, "ids =" + ids + "-- title =" + title + "-- type =" + type + "-- des =" + des + "\ n "); 22} 23}

② Gson Parsing

1) first install a GsonFormat plug-in AndroidStudio

 

  

2) create a new javaben class and press the combination key alt + insert to copy the complete json data to the editing box.

  

 

3) Add a gson dependency package

  

 

4) then generate the java ben in the specified format of Gson.

  

1 import java. util. list; 2 3/** 4 * by AdminHeJun. 5 * Time. 6 * mailbox: 1270960250@qq.com 7 * content: 8 * modification: 9 */10 11 public class NewsInfo {12 13 14 private int retcode; 15 private String header; 16 private List <DataBean> data; 17 18 public int getRetcode () {19 return retcode; 20} 21 22 public void setRetcode (int retcode) {23 this. retcode = retcode; 24} 25 26 public String getHeader () {27 return header; 28} 29 30 public void setHeader (String header) {31 this. header = header; 32} 33 34 public List <DataBean> getData () {35 return data; 36} 37 38 public void setData (List <DataBean> data) {39 this. data = data; 40} 41 42 public static class DataBean {43/** 44 * id: 1000045 * title: News 46 * type: 147 * des: this is an informative news article 111148 */49 50 private int id; 51 private String title; 52 private int type; 53 private String des; 54 55 public int getId () {56 return id; 57} 58 59 public void setId (int id) {60 this. id = id; 61} 62 63 public String getTitle () {64 return title; 65} 66 67 public void setTitle (String title) {68 this. title = title; 69} 70 71 public int getType () {72 return type; 73} 74 75 public void setType (int type) {76 this. type = type; 77} 78 79 public String getDes () {80 return des; 81} 82 83 public void setDes (String des) {84 this. des = des; 85} 86 87 @ Override88 public String toString () {89 return "DataBean {" + 90 "id =" + id + 91 ", title = '"+ title +' \'' + 92 ", type =" + type + 93 ", des = '"+ des +' \'' + 94 '}'; 95} 96} 97 98}

 

 

 

4) The next step is to use gson resolution.

  

1/** 2 * gson parses json data 3*4 * @ param s 5 */6 private void gsonUtil (String s) {7 // create a gson object 8 Gson gson = new Gson (); 9 // parse json data 10 NewsInfo newsInfo = gson. fromJson (s, NewsInfo. class); 11 12 String header = newsInfo. getHeader (); 13 int retcode = newsInfo. getRetcode (); 14 15 logs. I (TAG, "retcode =" + retcode + "---------- header =" + header); 16 17 // obtain the 18 List of data sets <NewsInfo. dataBean> data = newsInfo. getData (); 19 20 Log. I (TAG, "data ------->" + data. toString (); 21}

Print results

1 retcode = 200 ---------- header = http: // 192.168.126.26: 8080/news/a.jpg 2 3 4 5 data -------> [DataBean {id = 10000, title = 'News ', type = 1, des = 'This is a news article with connotation 1111 '},
DataBean {id = 10002, title = 'topic', type = 10, des = 'This is a meaningful news 222222 '},
DataBean {id = 10003, title = 'figure 2 ', type = 2, des =' This is a meaningful news 333333 '},
DataBean {id = 10006, title = 'figure 4', type = 2, des = 'This is a meaningful news 123456 '},
DataBean {id = 10008, title = 'figure 5', type = 2, des = 'This is a meaningful news 123456 '},
DataBean {id = 10003, title = 'figure 6', type = 2, des = 'This is a meaningful news dddd33 '},
DataBean {id = 10003, title = 'figure 7', type = 2, des = 'This is a meaningful news 3ssss33333 '},
DataBean {id = 10003, title = 'figure 8', type = 2, des = 'This is a meaningful news 33dddd33333 '},
DataBean {id = 10004, title = 'interface', type = 3, des = 'This is a meaningful news 444444'}]

Finally, paste the original json data

1 {2 "retcode": 200, 3 "data": [4 {5 "id": 10000, 6 "title": "news", 7 "type": 1, 8 "des": "This is a meaningful news article 1111" 9}, 10 {11 "id":, 12 "title": "topic", 13 "type ": 222222 "des": "This is a meaningful news article" 15}, 16 {17 "id":, 18 "title": "Image 2", 19 "type ": 333333 "des": "This is a meaningful news article" 21}, 22 {23 "id":, "title": "group chart 4", 25 "type ": 333333 "des": "This is a meaningful news article" 27}, 28 {29 "id":, 30 "title": "group chart 5", 31 "type ": 333333 "des": "This is an informative news article" 33}, 34 {35 "id":, 36 "title": "group chart 6", 37 "type ": "des": "This is a meaningful news ddddd33" 39}, 40 {41 "id":, 42 "title": "group chart 7", 43 "type ": "des": "This is an informative news article 3ssss33333" 45}, 46 {47 "id":, 48 "title": "group chart 8", 49 "type ": "des": "This is a meaningful news 33dddd33333" 51}, 52 {53 "id":, 54 "title": "interaction", 55 "type ": 3,56 "des": "This is an informative news article 444444" 57} 58], 59 "header": "http: // 192.168.126.26: 8080/news/a.jpg "60 61 62}

Okay, this is the end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.