问题描述
我使用的是 WSO2 ESB 4.8.0,在将 xml(从 xslt 输出)转换为 json 时遇到以下问题:
I'm using WSO2 ESB 4.8.0 and I have the following problems converting an xml (from an xslt output) to a json:
带数字的字符串会自动转换为整数元素:"orderId": 10000 ( 我希望它是字符串 "orderId": "10000" )
strings with numbers automatically get converted as integer elements: "orderId": 10000 ( I want it to be string "orderId": "10000" )
xml 列表如果有一个元素,则作为 json 对象出现;如果存在多个元素,则作为 json 数组出现.我如何告诉转换器此特定项目必须始终是一个数组"?
xml list come as json object if there is one element and as json array if more than one element is present..How can I tell the converter "this particular item must always be an array" ?
我使用 xslt 来构建 xml(因为我有 variuos xml 从一些外部系统调用中合并),然后使用属性 messageType application/json.
I'm using xslt to build the xml (as I have variuos xml to merge from some external system calls) and then use the property messageType application/json.
我不能使用payloadFactory,因为我有一些逻辑来实现和使用带有javascript指令的脚本中介器,这对我来说就像一个最糟糕的解决方案.
I can't use the payloadFactory since I have some logics to implement and use the script mediator with javascript instructions semms to me like a worst solution.
提前致谢.
推荐答案
要完成此操作,您可以更改 JSON 消息格式器.转到 [ESB 主页]/repository/conf/axis2/axis2.xml.
To get this done you can change the JSON Message Formatters.Go to [ESB Home]/repository/conf/axis2/axis2.xml.
更改 JSON 消息格式器而不是 JsonStreamFormatter 使用 org.apache.axis2.json.JSONMessageFormatter
Change the JSON Message Formattersinstead of JsonStreamFormatter use org.apache.axis2.json.JSONMessageFormatter
<!--messageFormatter contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamFormatter"/-->
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/>
注意.这里所有的值都是字符串.
Note. here all values would be strings.
这篇关于wso2 esb - XML 到 JSON 转换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!