最近项目需要跟客户对接一个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 宁德