我在wso2企业集成控制台上部署了一个API服务,该服务在数据库上为select发送请求。 它返回层次结构的数据 xml格式:
<cdcList xmlns="http://ws.apache.org/ns/synapse">
<cdc>
<idCdc xmlns="">2</idCdc>
<idCdcParent xmlns=""/>
<cdcName xmlns="">Roma</cdcName>
<order xmlns="">1</order>
<isUsed xmlns="">false</isUsed>
<cdcList>
<cdc>
<idCdc xmlns="">5</idCdc>
<idCdcParent xmlns="">2</idCdcParent>
<cdcName xmlns="">Progetti</cdcName>
<order xmlns="">2</order>
<isUsed xmlns="">false</isUsed>
<cdcList>
<cdc>
<idCdc xmlns="">17</idCdc>
<idCdcParent xmlns="">5</idCdcParent>
<cdcName xmlns="">testGP</cdcName>
<order xmlns="">1</order>
<isUsed xmlns="">false</isUsed>
<cdcList/>
</cdc>
<cdc>
<idCdc xmlns="">18</idCdc>
<idCdcParent xmlns="">5</idCdcParent>
<cdcName xmlns="">testGPS</cdcName>
<order xmlns="">2</order>
<isUsed xmlns="">false</isUsed>
<cdcList/>
</cdc>
</cdcList>
</cdc>
</cdcList>
</cdc>
<cdc>
<idCdc xmlns="">3</idCdc>
<idCdcParent xmlns=""/>
<cdcName xmlns="">Milano</cdcName>
<order xmlns="">4</order>
<isUsed xmlns="">false</isUsed>
<cdcList>
<cdc>
<idCdc xmlns="">7</idCdc>
<idCdcParent xmlns="">3</idCdcParent>
<cdcName xmlns="">l</cdcName>
<order xmlns="">4</order>
<isUsed xmlns="">false</isUsed>
<cdcList/>
</cdc>
</cdcList>
</cdc>
<cdc>
<idCdc xmlns="">4</idCdc>
<idCdcParent xmlns=""/>
<cdcName xmlns="">Napoli</cdcName>
<order xmlns="">5</order>
<isUsed xmlns="">false</isUsed>
<cdcList>
<cdc>
<idCdc xmlns="">9</idCdc>
<idCdcParent xmlns="">4</idCdcParent>
<cdcName xmlns="">cccc</cdcName>
<order xmlns="">6</order>
<isUsed xmlns="">false</isUsed>
<cdcList/>
</cdc>
</cdcList>
</cdc>
</cdcList>
该服务使用Apache Axis2引擎的application / json,它将xml的输出转换为json。 问题是,如果父对象有多个子节点它运行良好但如果只有一个或没有一个子节点它不返回井json格式输出。 我试图检查Apache Axis2引擎的java类,找到它管理json数组中的括号但我找不到解决方案。 这是API服务的输出:
{"cdcList":{"cdc":[{"idCdc":2,"idCdcParent":null,"cdcName":"Roma","order":1,"isUsed":false,"cdcList":{"cdc":{"idCdc":5,"idCdcParent":2,"cdcName":"Progetti","order":2,"isUsed":false,"cdcList":{"cdc":[{"idCdc":17,"idCdcParent":5,"cdcName":"testGP","order":1,"isUsed":false,"cdcList":null},{"idCdc":18,"idCdcParent":5,"cdcName":"testGPS","order":2,"isUsed":false,"cdcList":null}]}}}},{"idCdc":3,"idCdcParent":null,"cdcName":"Milano","order":4,"isUsed":false,"cdcList":{"cdc":{"idCdc":7,"idCdcParent":3,"cdcName":"l","order":4,"isUsed":false,"cdcList":null}}},{"idCdc":4,"idCdcParent":null,"cdcName":"Napoli","order":5,"isUsed":false,"cdcList":{"cdc":{"idCdc":9,"idCdcParent":4,"cdcName":"cccc","order":6,"isUsed":false,"cdcList":null}}}]}}
这就是我希望的方式:
{
"cdcList": [{
"idCdc": 2,
"idCdcParent": null,
"cdcName": "Roma",
"order": 1,
"isUsed": false,
"cdcList": [{
"idCdc": 5,
"idCdcParent": 2,
"cdcName": "Progetti",
"order": 2,
"isUsed": false,
"cdcList": [{
"idCdc": 17,
"idCdcParent": 5,
"cdcName": "testGP",
"order": 1,
"isUsed": false,
"cdcList": null
},
{
"idCdc": 18,
"idCdcParent": 5,
"cdcName": "testGPS",
"order": 2,
"isUsed": false,
"cdcList": null
}
]
}]
},
{
"idCdc": 3,
"idCdcParent": null,
"cdcName": "Milano",
"order": 4,
"isUsed": false,
"cdcList": [{
"idCdc": 7,
"idCdcParent": 3,
"cdcName": "l",
"order": 4,
"isUsed": false,
"cdcList": null
}]
},
{
"idCdc": 4,
"idCdcParent": null,
"cdcName": "Napoli",
"order": 5,
"isUsed": false,
"cdcList": [{
"idCdc": 9,
"idCdcParent": 4,
"cdcName": "cccc",
"order": 6,
"isUsed": false,
"cdcList": null
}]
}
]
}
注意括号和缺少“cdc”项目。 谢谢。
答案 0 :(得分:0)
您可能知道问题是XML to JSON转换器无法判断单个XML元素是否是有意的并且应该转换为JSON对象,或者它是一个列表,并且应该将该对象包装在列表中。
最简单的方法是使用XSLT转换在每个cdcList元素之前添加xml-multiple处理指令。
在将XML转换为JSON
之前,您希望XML看起来像这样<?xml-multiple cdcList?>
<cdcList xmlns="http://ws.apache.org/ns/synapse">
<cdc>
<idCdc xmlns="">2</idCdc>
<idCdcParent xmlns=""/>
<cdcName xmlns="">Roma</cdcName>
<order xmlns="">1</order>
<isUsed xmlns="">false</isUsed>
<?xml-multiple cdcList?>
<cdcList>
<cdc>
<idCdc xmlns="">5</idCdc>
<idCdcParent xmlns="">2</idCdcParent>
<cdcName xmlns="">Progetti</cdcName>
<order xmlns="">2</order>
<isUsed xmlns="">false</isUsed>
<?xml-multiple cdcList?>
<cdcList>
<cdc>
<idCdc xmlns="">17</idCdc>
<idCdcParent xmlns="">5</idCdcParent>
... etc