你的位置:首页 > 操作系统

[操作系统]利用android studio gsonformat插件快速解析复杂json

       在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());

       看看解析的结果吧

 

 

       是不是很方便呢