Hutool XML 转JSON 后 parseArray踩坑
XML转JSON
public static String getSingleAcString() {String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +"<list>\n" +"<acBasicInfo>\n" +" <label>JingChang-AC-A</label>\n" +" <sysName>JingChang-AC-A</sysName>\n" +" <status>0</status>\n" +" <ipAddress>10.153.129.64</ipAddress>\n" +" <type>H3C WX6100EWPX</type>\n" +" <pingStatus>1</pingStatus>\n" +" <macAddress>00:23:89:16:51:80</macAddress>\n" +" <softwareVersion>Release 2109P05</softwareVersion>\n" +" <hardwareVersion></hardwareVersion>\n" +" <serialId></serialId>\n" +" <onlineApCount>16</onlineApCount>\n" +" <onlineClientCount>19</onlineClientCount>\n" +"</acBasicInfo>\n" +"</list>";return xml;}
String string = XmlExample.getSingleAcString();JSONObject json = JSONUtil.parseFromXml(string);JSONObject list = (JSONObject) json.get("list");JSONArray jsonArray = JSONUtil.parseArray(list.get("acBasicInfo"));List<AcPerfInfo> acPerfInfos = JSONUtil.toList(jsonArray, AcPerfInfo.class);acPerfInfos.stream().forEach(System.out::println);
想将acBasicInfo转成list集合
理想状态是 list里面有一个 AcPerfInfo对象 实际上变成了12 的对象 里面有12 个属性
解决方案
判断标签出现次数 出现次数大于 1 就 用toList方法 小于 就转为对象 再加入集合
public static int appearNumber(String srcText, String findText) {int count = 0;int index = 0;while ((index = srcText.indexOf(findText, index)) != -1) {index = index + findText.length();count++;if (count >= 2){return count;}}return count;}
JSONObject json = JSONUtil.parseFromXml(acResult);JSONObject list = (JSONObject) json.get("list");List<AcBasicInfoResVo> acList = new ArrayList<>();int amount = appearNumber(acResult, "</acBasicInfo>");//amount 为 1 时 有坑 单独处理if (amount == 1) {JSONObject single = (JSONObject) list.get("acBasicInfo");AcBasicInfoResVo acBasicInfoResVo = single.toBean(AcBasicInfoResVo.class);acList.add(acBasicInfoResVo);} else {JSONArray jsonArray = JSONUtil.parseArray(list.get("acBasicInfo"));acList = JSONUtil.toList(jsonArray, AcBasicInfoResVo.class);}
Hutool XML 转JSON 后 parseArray踩坑相关推荐
- mybatis mapper.xml dtd_全栈开发踩坑之路4-用MyBatis实现服务
1.前言 上一篇文章介绍了如何设计后端的Mysql数据库:Alex Wang:全栈开发踩坑之路3-MySql数据库设计,本文介绍如何用MyBatis实现后端服务. 本后端项目的Github地址(撰写中 ...
- swiper4自动轮播切换手动触碰后停止踩坑——属性disableOnInteraction
swiper4轮播设置autoplay自动切换后,即默认设置: <script> var mySwiper = new Swiper('.swiper-container', {autop ...
- 【golang程序包推荐分享】分享亿点点golang json操作及myJsonMarshal程序包开发的踩坑经历 :)
目录[阅读时间:约5分钟] 一.概述 1.Json的作用 2.Go官方 encoding/json 包 3. golang json的主要操作 二.Json Marshal:将数据编码成json字符串 ...
- Android 解决XXX Layout leaked 使用Navigation 踩坑 XML内存泄漏
Android 解决XXX Layout leaked 使用Navigation 踩坑 XML内存泄漏 报错日志 排查过程 泄漏原因 解决方案 最近维护一个项目,一个内存泄漏的的原因查了很久,这里记录 ...
- go nil json.marshal 完是null_字节跳动踩坑记#3:Go服务灵异panic
这个坑比较新鲜,刚填完,还冒着冷气. - 1 - 在字节跳动,我们服务的所有 log 都通过统一的日志库采集到流式日志服务.落地 ES 集群,配上字节云超(sang)级(xin)强(bing)大(ku ...
- 华为服务器安装esxi找不到硬盘,Vmware Esxi 6.7设置阵列卡直通后找不到存储踩坑总结...
Vmware版本:Vmware Esxi 6.7 U2 服务器型号: DELL R720XD 阵列卡:R710mini 某天因为需要某个硬盘直通,于是设置了一下阵列卡直通.服务器重启后,发现vmwar ...
- “少踩坑”,拿到offer后,我们可以做什么?
原创: Kevin改变世界的点滴 Kevin改变世界的点滴 昨天 马上要到春季了,在这个金三银时的季节,今天聊聊如何选择offer 但针对企业中的"坑",产品人学会选择offer也 ...
- linux添加阵列后看不到home下文件,Vmware Esxi 6.7设置阵列卡直通后找不到存储踩坑总结...
Vmware版本:Vmware Esxi 6.7 U2 服务器型号: DELL R720XD 阵列卡:R710mini 某天因为需要某个硬盘直通,于是设置了一下阵列卡直通.服务器重启后,发现vmwar ...
- HP Zbook 15重装ubantu18后,安装Nvidia驱动(踩坑实录,不推荐尝试)
电脑型号:HP Zbook 15 G6 显卡:nvidia Quadro T1000 安装方式: 使用官方的NVIDIA驱动进行手动安装(经过安装方式和结果对比,强烈不推荐手动安装的方式,这就是一篇 ...
最新文章
- android+抄袭+苹果,别再把苹果捧上神坛了,其实苹果和安卓都在借鉴抄袭这个品牌!...
- LeetCode 438. Find All Anagrams in a String--字符串-滑动窗口--C++,Python解法
- 贵州二本好的计算机专业,官方支持贵州大学创建双一流大学,贵州唯一的211,二本也有机会...
- [IE技巧] IE Security Zone 的注册表设置
- nginx conf 文件
- python编程规范
- Ubuntu16.04 安装R与RStudio
- VTK:Picking之HighlightPickedAct
- LeetCode 258 Add Digits
- zookeeper 安装和使用
- 项目开发日志:Build AssetBundle——SpriteAtlas(已解惑)
- sublime-编辑器的使用-基本
- winform窗体中嵌入显示Excel文件
- 最大公约数GCD的三种算法程序
- 零基础学习 Python 之前期准备
- iOS开发之iPhone手机轻松获取UDID的六种方式
- Vue插件element-ui安装时报错error An unexpected error occurred: “https://registry.npmjs.org/element-ui: conn
- 解决 “ImportError: attempted relative import with no known parent package“ 问题
- 猿人学第五题上---代码
- semiplannar 什么是_图像识别VPU——易用的嵌入式AI支持深度学习平台介绍