json-lib中xml转json的格式区别

今天使用json-lib的xml转json功能时,有两种类型数据格式会出现不同的结果

一、带制表符的xml数据格式

    这种格式在嵌套格式数据时,转换后的数据会将嵌套中一级的去掉,如下面的代码会httpsql去掉

    下面数据的第一个childrens有两个子节点,第二个childrens有一个节点

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <https>
        <http>
            <!-- URL -->
            <url></url>
            <!-- 请求开始时间 -->
            <starttime>2</starttime>
            <!-- 用户代理 -->
            <useragent>3</useragent>
            <!-- 请求次数 -->
            <httpreqnum>4</httpreqnum>
            <!-- 耗时 -->
            <timenum>5</timenum>
            <!-- 最大并发 -->
            <concurrencynum>6</concurrencynum>
            <!-- Jdbc执行数 -->
            <jdbcexenum>7</jdbcexenum>
            <!-- Jdbc出错数 -->
            <jdbcerrornum>8</jdbcerrornum>
            <!-- Jdbc时间 -->
            <jdbctime>9</jdbctime>
            <!-- 事务提交数 -->
            <commitnum>10</commitnum>
            <!-- 事务回滚数 -->
            <rollbacknum>11</rollbacknum>
            <!-- 读取行数 -->
            <readnum>12</readnum>
            <!-- 更新行数 -->
            <updatenum>13</updatenum>
            <!-- 请求IP -->
            <ipaddr>14</ipaddr>
            <!-- 登录名称 -->
            <loginname>15</loginname>
            <!-- 登录账户 -->
            <loginusername>16</loginusername>
            <childrens>
                <httpsql>
                    <!-- sql语句 -->
                    <sql>1</sql>
                    <!-- 耗时 -->
                    <exesqltime>2</exesqltime>
                </httpsql>
                <httpsql>
                    <!-- sql语句 -->
                    <sql>3</sql>
                    <!-- 耗时 -->
                    <exesqltime>4</exesqltime>
                </httpsql>
            </childrens>
        </http>
        <http>
            <!-- URL -->
            <url>1</url>
            <!-- 请求开始时间 -->
            <starttime>2</starttime>
            <!-- 用户代理 -->
            <useragent>3</useragent>
            <!-- 请求次数 -->
            <httpreqnum>4</httpreqnum>
            <!-- 耗时 -->
            <timenum>5</timenum>
            <!-- 最大并发 -->
            <concurrencynum>6</concurrencynum>
            <!-- Jdbc执行数 -->
            <jdbcexenum>7</jdbcexenum>
            <!-- Jdbc出错数 -->
            <jdbcerrornum>8</jdbcerrornum>
            <!-- Jdbc时间 -->
            <jdbctime>9</jdbctime>
            <!-- 事务提交数 -->
            <commitnum>10</commitnum>
            <!-- 事务回滚数 -->
            <rollbacknum>11</rollbacknum>
            <!-- 读取行数 -->
            <readnum>12</readnum>
            <!-- 更新行数 -->
            <updatenum>13</updatenum>
            <!-- 请求IP -->
            <ipaddr>14</ipaddr>
            <!-- 登录名称 -->
            <loginname>15</loginname>
            <!-- 登录账户 -->
            <loginusername>16</loginusername>
            <childrens>
                <httpsql>
                    <!-- sql语句 -->
                    <sql>3</sql>
                    <!-- 耗时 -->
                    <exesqltime>4</exesqltime>
                </httpsql>
            </childrens>
        </http>
    </https>
</root>

    转换后的结果

[[{"url":[],"starttime":"2","useragent":"3","httpreqnum":"4","timenum":"5","concurrencynum":"6","jdbcexenum":"7","jdbcerrornum":"8","jdbctime":"9","commitnum":"10","rollbacknum":"11","readnum":"12","updatenum":"13","ipaddr":"14","loginname":"15","loginusername":"16","childrens":[{"sql":"1","exesqltime":"2"},{"sql":"3","exesqltime":"4"}]},{"url":"1","starttime":"2","useragent":"3","httpreqnum":"4","timenum":"5","concurrencynum":"6","jdbcexenum":"7","jdbcerrornum":"8","jdbctime":"9","commitnum":"10","rollbacknum":"11","readnum":"12","updatenum":"13","ipaddr":"14","loginname":"15","loginusername":"16","childrens":[{"sql":"3","exesqltime":"4"}]}]]

 二、压缩后的xml格式数据格式

    使用压缩工具将xml数据压缩成为一行后,转换的结果

[[{"url":[],"starttime":"2","useragent":"3","httpreqnum":"4","timenum":"5","concurrencynum":"6","jdbcexenum":"7","jdbcerrornum":"8","jdbctime":"9","commitnum":"10","rollbacknum":"11","readnum":"12","updatenum":"13","ipaddr":"14","loginname":"15","loginusername":"16","childrens":[{"sql":"1","exesqltime":"2"},{"sql":"3","exesqltime":"4"}]},{"url":"1","starttime":"2","useragent":"3","httpreqnum":"4","timenum":"5","concurrencynum":"6","jdbcexenum":"7","jdbcerrornum":"8","jdbctime":"9","commitnum":"10","rollbacknum":"11","readnum":"12","updatenum":"13","ipaddr":"14","loginname":"15","loginusername":"16","childrens":{"httpsql":{"sql":"3","exesqltime":"4"}}}]]

区别在于childrens的结果,压缩后转换会出现httpsql的节点,先记录,后续再查看源码

你可能感兴趣的:(json-lib)