最近項目需要跟客戶對接一個webservice接口,客戶那傳json串過來,屬於比較復雜的json串,這里跟大家分享下我項目中所用的解析方法:
該方法需要以下jar
package com.test; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * 描述 * @author 小當家 * @created 2017年12月7日 上午9:08:03 */ public class TestJson { /** * * 描述: 轉換復雜的JSON對象為 Map對象 * @author 小當家 * @created 2017年12月7日 上午9:15:12 * @param jsonStr * @return */ @SuppressWarnings("unused") private static Map<String, Object> parseJSON2Map(String jsonStr) { Map<String, Object> map = new HashMap<String, Object>(); // 最外層解析 JSONObject json = JSONObject.fromObject(jsonStr); for (Object k : json.keySet()) { Object v = json.get(k); // 如果內層還是數組的話,繼續解析 if (v instanceof JSONArray) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator<JSONObject> it = ((JSONArray) v).iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2.toString())); } map.put(k.toString(), list); } else { map.put(k.toString(), v); } } return map; } public static void main(String[] args) { String jsonString = "{\"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\"}]}]}"; Map<String, Object> map= parseJSON2Map(jsonString); System.out.println(map.get("payCnt"));//獲取電子票據數目 List list = (List)map.get("payInfo");//獲取電子票據信息(List類型) Map t = (Map)list.get(0);//獲取第一行記錄,Map類型 Map mainMap = (Map)t.get("payInfoMain");//獲取電子票據主要信息,Map類型 String user = (String)mainMap.get("payer");//取出繳款人 System.out.println(user); } }
經過測試后台打印:3 寧德