Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 利用android studio gsonformat插件快速解析復雜json,gsonformatjson

利用android studio gsonformat插件快速解析復雜json,gsonformatjson

編輯:關於android開發

利用android studio gsonformat插件快速解析復雜json,gsonformatjson


       在android開發過程中,難免會遇到json解析,在這篇文章中為你快速解析復雜的json。

       首先,在android studio中安裝gsonformat插件。

       點擊File->Settings->Plugins

 

       安裝後重啟android studio

       接下來,新建工程,導入gson。

       File->Project Structure->

       點擊“+”,->Library dependency

 

       選擇GSON

       解析來就是解析json了。

       網上json例子:

{"payCnt":3, "payInfo":[ {"payInfoMain":{"ordNum":"201206010000001","transSite":"","transBankNo":"TEPB","bankAcctNo":"010","totAmt":"30","billChkCode":"3316","tollDeptNo":"NDZSXH000","busType":"FJFS","other1":"remark1","other2":"remark2","bankName":"模擬銀行2","transSeq":"20120601000000001","tranType":"","siteCode":"200001","eBillVerNo":"000001","tollDeptName":"寧德市會計專業技術考試領導小組","itemCnt":1,"tranUser":"","payer":"寧德","eBillNo":"201206010000000136","transBankName":"模擬銀行2","transAcctNo":"010","siteName":"國家司法考試網","ordDate":"20120601"}, "eBillNo":"201206010000000136", "payInfoItem":[{"amt":"30","chrgStd":"1","cnt":"1","chrgName":"初級網絡工程師報名費","msrUint":"元/科","chrgCode":"KA460001"}]}, {"payInfoMain":{"ordNum":"201206010000001","transSite":"","transBankNo":"TEPB","bankAcctNo":"010","totAmt":"10","billChkCode":"2258","tollDeptNo":"777516727","busType":"FJFS","other1":"remark1","other2":"remark2","bankName":"模擬銀行2","transSeq":"20120601000000001","tranType":"","siteCode":"200001","eBillVerNo":"000001","tollDeptName":"福建省省會計管理處","itemCnt":1,"tranUser":"","payer":"繳款人","eBillNo":"201206010000000134","transBankName":"模擬銀行2","transAcctNo":"010","siteName":"國家司法考試網","ordDate":"20120601"}, "eBillNo":"201206010000000134", "payInfoItem":[{"amt":"10","chrgStd":"1","cnt":"1","chrgName":"交通罰沒","msrUint":"元/科","chrgCode":"460"}]}, {"payInfoMain":{"ordNum":"201206010000001","transSite":"","transBankNo":"TEPB","bankAcctNo":"010","totAmt":"20","billChkCode":"0198","tollDeptNo":"003604520","busType":"FJFS","other1":"remark1","other2":"remark2","bankName":"模擬銀行2","transSeq":"20120601000000001","tranType":"","siteCode":"200001","eBillVerNo":"000001","tollDeptName":"福州市財政局會計管理處","itemCnt":1,"tranUser":"","payer":"福州","eBillNo":"201206010000000135","transBankName":"模擬銀行2","transAcctNo":"010","siteName":"國家司法考試網","ordDate":"20120601"}, "eBillNo":"201206010000000135", "payInfoItem":[{"amt":"20","chrgStd":"1","cnt":"1","chrgName":"會計從業資格證考務費","msrUint":"元/科","chrgCode":"BA460"}]}]}

 

       新建一個Bean,右鍵Gerenate->GsonFormat,將json串復制進去

 

 

 

       注意:如果需要新建內部類的話需要與字段名稱相同

 

       創建完Bean類後,就可以使用gson來解析了。

       在MainActivity中調用

       Gson mGson=new Gson();

       Bean bean=mGson.fromJson(json,Bean.class);

         Log.i("Debug",bean.getPayInfo().get(0).getPayInfoItem().get(0).getChrgName());

       看看解析的結果吧

 

 

       是不是很方便呢

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved