Json文件转Map(三)之获取嵌套Map值 |
您所在的位置:网站首页 › json报文转map › Json文件转Map(三)之获取嵌套Map值 |
说明
在上一章节的博客中,我们已经学习了如何解析嵌套的Json,将获取嵌套Json的节点。那么在这一章节中,如何通过以下的节点格式去取值。 index8=results>index>index7>index7=results>index>index=results>index2=results>results=null 分析及实现既然我们已经解析了JSON文件,并且转化了Map,而且还拿到了嵌套Json的节点,现在无非就是遍历Map,然后重新取值,这个逻辑应该不难。代码如下: /** * 根据json报文和结点获取相应的值 * @param map 已经解析过的Json * @param listNode 要获取的字段在json中的节点路径(用">"分割) * @return */ public static List gainJsonMapValue(Map jsonMap,String stringNode){ List resultList = new ArrayList(); String[] nodeArray = stringNode.split(">"); for (int i = 0; i < nodeArray.length; i++) { if (jsonMap.get(nodeArray[i]) instanceof List) { if (i < nodeArray.length&& null != jsonMap.get(nodeArray[i]) && "" != jsonMap.get(nodeArray[i])) { Map mapList = jsonMap; if (((List)mapList.get(nodeArray[i])).size() > 0) { Map map = new HashMap(); map.put(nodeArray[i], ((List)mapList.get(nodeArray[i]))); resultList.add(map); } } break; } else{ if (i < nodeArray.length - 1 && null != jsonMap.get(nodeArray[i]) && "" != jsonMap.get(nodeArray[i])) { jsonMap = (Map) jsonMap.get(nodeArray[i]); }else{ Map resultMap = new HashMap(); resultMap.put(nodeArray[i],jsonMap.get(nodeArray[i])+"");//nodeArray resultList.add(resultMap); } } } // System.out.println("#####"+resultList); return resultList; }小结 这篇博客只是简单的说明一下如何去取已经解析出来的Map节点。在下一篇博客中,笔者将会把所有代码都分享出来。 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |