当前位置: 首页 > 工具软件 > ka-map > 使用案例 >

java复杂map转json_转换复杂的JSON对象为 Map对象

卢伟志
2023-12-01

packagecom.test;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;/*** 描述

*@author小当家

* @created 2017年12月7日 上午9:08:03*/

public classTestJson {/***

* 描述: 转换复杂的JSON对象为 Map对象

*@author小当家

* @created 2017年12月7日 上午9:15:12

*@paramjsonStr

*@return

*/@SuppressWarnings("unused")private static MapparseJSON2Map(String jsonStr) {

Map map = new HashMap();//最外层解析

JSONObject json =JSONObject.fromObject(jsonStr);for(Object k : json.keySet()) {

Object v=json.get(k);//如果内层还是数组的话,继续解析

if (v instanceofJSONArray) {

List> list = new ArrayList>();

Iterator 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);

}

}returnmap;

}public static voidmain(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 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);

}

}

 类似资料: