java复杂map转json_转换复杂的JSON对象为Map对象st;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map 描述
*@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 (k);//如果内层还是数组的话,继续解析
if (v instanceofJSONArray) {
List> list = new ArrayList>();
Iterator it =((JSONArray) v).iterator();while(it.hasNext()) {
JSONObject ();
list.add(String()));
}
map.String(), list);
}else{
map.String(), v);
}
}returnmap;
}public static voidmain(String[] args) {
String jsonString= "{\"payCnt\":3,\"payInfo\":[{\"payInfoMain\":
{\"ordNum\":\"201206010000001\",\"transSite\":\"\",\"transBankNo\":\"TEPB\",\"bankAcctNo\":\"010\",\"totAmt\":\"30\",\"billCh 拟银⾏
json转换对象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\",\"billCh 拟银⾏
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\",\"billCh 拟银⾏
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.("payCnt"));//获取电⼦票据数⽬
List list = (("payInfo");//获取电⼦票据信息(List类型)
Map t = ((0);//获取第⼀⾏记录,Map类型
Map mainMap = (("payInfoMain");//获取电⼦票据主要信息,Map类型
String user = (("payer");//取出缴款⼈
System.out.println(user);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论