Json解析與Gson解析

来源:http://www.cnblogs.com/hejiaoshou/archive/2017/09/03/7471036.html
-Advertisement-
Play Games

本文主要介紹json最原始的解析與google提供的gson工具類解析 ①json解析 ②gson解析 1)首先在AndroidStudio中安裝一個GsonFormat插件 2)新建一個javaben類然後按下組合鍵alt+insert 把完整的json數據拷貝到編輯框中 3)添加gson的依賴包 ...


  本文主要介紹json最原始的解析與google提供的gson工具類解析

 ①json解析

 1 /**
 2      * 普通的json解析
 3      * @param s
 4      * @throws JSONException
 5      */
 6     private void jsonJieXi(String s) throws JSONException {
 7         //創建json對象
 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解析

  1)首先在AndroidStudio中安裝一個GsonFormat插件

 

  

  2)新建一個javaben類然後按下組合鍵alt+insert                  把完整的json數據拷貝到編輯框中 

  

 

  3)添加gson的依賴包

  

 

  4)然後生成Gson指定格式的java ben

  

 1 import java.util.List;
 2 
 3 /**
 4  * 作者:AdminHeJun.
 5  * 時間:2017/9/3 19:28.
 6  * 郵箱:[email protected]
 7  * 內容:
 8  * 修改:
 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 : 10000
45          * title : 新聞
46          * type : 1
47          * des : 這是一條有內涵的新聞1111
48          */
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         @Override
88         public String toString() {
89             return "DataBean{" +
90                     "id=" + id +
91                     ", title='" + title + '\'' +
92                     ", type=" + type +
93                     ", des='" + des + '\'' +
94                     '}';
95         }
96     }
97 
98 }

 

 

 

  4)接下來就是使用gson解析啦

  

 1 /**
 2      * gson解析json數據
 3      *
 4      * @param s
 5      */
 6     private void gsonUtil(String s) {
 7         //創建一個gson對象
 8         Gson gson = new Gson();
 9         //解析json數據
10         NewsInfo newsInfo = gson.fromJson(s, NewsInfo.class);
11 
12         String header = newsInfo.getHeader();
13         int retcode = newsInfo.getRetcode();
14 
15         Log.i(TAG, "retcode=" + retcode + "----------header=" + header);
16         
17         //得到data數據的集合
18         List<NewsInfo.DataBean> data = newsInfo.getData();
19 
20         Log.i(TAG, "data------->" + data.toString());
21     }

列印結果

1 retcode=200----------header=http://192.168.126.26:8080/news/a.jpg
2 
3 
4 
5 data------->[DataBean{id=10000, title='新聞', type=1, des='這是一條有內涵的新聞1111'},
DataBean{id=10002, title='專題', type=10, des='這是一條有內涵的新聞222222'},
DataBean{id=10003, title='組圖2', type=2, des='這是一條有內涵的新聞333333'},
DataBean{id=10006, title='組圖4', type=2, des='這是一條有內涵的新聞333333'},
DataBean{id=10008, title='組圖5', type=2, des='這是一條有內涵的新聞333333'},
DataBean{id=10003, title='組圖6', type=2, des='這是一條有內涵的新聞ddddd33'},
DataBean{id=10003, title='組圖7', type=2, des='這是一條有內涵的新聞3ssss33333'},
DataBean{id=10003, title='組圖8', type=2, des='這是一條有內涵的新聞33dddd33333'},
DataBean{id=10004, title='互動', type=3, des='這是一條有內涵的新聞444444'}]

最後貼上原始的json數據

 1 {
 2     "retcode": 200,
 3     "data": [
 4         {
 5             "id": 10000,
 6             "title": "新聞",
 7             "type": 1,
 8         "des":"這是一條有內涵的新聞1111"        
 9         },
10         {
11             "id": 10002,
12             "title": "專題",
13             "type": 10,
14             "des":"這是一條有內涵的新聞222222"    
15         },
16         {
17             "id": 10003,
18             "title": "組圖2",
19             "type": 2,
20             "des":"這是一條有內涵的新聞333333"    
21         },
22      {
23             "id": 10006,
24             "title": "組圖4",
25             "type": 2,
26             "des":"這是一條有內涵的新聞333333"    
27         },
28      {
29             "id": 10008,
30             "title": "組圖5",
31             "type": 2,
32             "des":"這是一條有內涵的新聞333333"    
33         },
34      {
35             "id": 10003,
36             "title": "組圖6",
37             "type": 2,
38             "des":"這是一條有內涵的新聞ddddd33"    
39         },
40      {
41             "id": 10003,
42             "title": "組圖7",
43             "type": 2,
44             "des":"這是一條有內涵的新聞3ssss33333"    
45         },
46      {
47             "id": 10003,
48             "title": "組圖8",
49             "type": 2,
50             "des":"這是一條有內涵的新聞33dddd33333"    
51         },
52         {
53             "id": 10004,
54             "title": "互動",
55             "type": 3,
56              "des":"這是一條有內涵的新聞444444"    
57         }
58     ],
59     "header":"http://192.168.126.26:8080/news/a.jpg"
60     
61     
62 }

好啦操作到此結束


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 表的操作: 1.表的創建: create table if not exists table_name(欄位定義); 例子: create table if not exists user(id int auto_increment, uname varchar(20), address varch ...
  • 一、對資料庫的操作 1、創建一個庫 create database 庫名; 創建帶有編碼的:create database 庫名 character set 編碼; 查看編碼:show create database 庫名; 2、刪除一個庫 drop database 庫名; 3、使用庫 use 庫 ...
  • 避免ANR異常 不要在主線程中執行耗時的代碼,不然很容易出現anr錯誤。 原因: 解決方法: ...
  • android中進程的優先順序 ...
  • 內容觀察者實現簡訊監聽 通過內容觀察者監聽簡訊是否發生變化,如果發生變化,就用內容提供者讀取簡訊的內容。 原理: 當簡訊發生變化(比如說來簡訊),簡訊應用就會通知內容觀察者我的簡訊發生了變化,並把變化位置簡訊的uri傳過來,有uri之後,內容提供者就可以很方便的讀取簡訊內容了,不過肯定要保證許可權夠。 ...
  • 虛擬簡訊 ...
  • 內容提供者 參考: Android四大組件之內容提供者--ContentProvider - java小兵 - CSDN博客http://blog.csdn.net/wodewutai17quiet/article/details/46670597 Android四大組件之內容提供者--Conten ...
  • 在Android開發中難免會遇到大量的數據載入到ListView中進行顯示, 然後其中最重要的數據傳遞橋梁Adapter適配器是常用的,隨著市場的需 求變化ListView'條目中的內容是越來越多這就需要程式員來自定義適配器, 而關鍵的就是適配器的優化問題,適配器沒有優化好往往就會造成OOM (記憶體 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...