WSO2中XML到JSON的转换 [英] xml to json conversion in wso2

查看:344
本文介绍了WSO2中XML到JSON的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在wso2中使用XSLT介体将XML转换为Json时,出现无法将有效载荷编写为JSON"的消息.错误.谁能帮我解决这个问题.

When I am trying to convert XML to Json using XSLT mediator in wso2 I am getting "Payload could not be written as JSON." error. Can anyone help to me resolve this.

预先感谢

推荐答案

为什么不使用有效负载工厂?这是将xml转换为Json的最佳方法.

Why you do not use payload factory? this is a best way for converting xml to Json.

例如:

<payloadFactory media-type="json">
    <format>{ "error": "0", "message": "$1", "data": $2 }</format>
        <args>
            <arg evaluator="json" expression="$.USER.description"/>
            <arg evaluator="json" expression="$.USER"/>
        </args>
</payloadFactory>
<property name="ContentType" value="application/json" scope="axis2"/>

或使用此属性发送消息:

Or use this property for send message:

<property name="messageType" value="application/json" scope="axis2"/>

media-type属性指定是以XML还是JSON格式化消息. 在此示例中,我使用JSON转换此xml:

The media-type attribute specifies whether to format the message in XML or JSON. In this example i use JSON to convert this xml:

<USER>
    <description>some Notes</description>
    <others></others>
</USER>

和ESB结果:

{
    "error" : "0",
    "message" : "Some notes",
    "data"  : {
        "description" : "Some notes",
        "others" : ""
    }
}

有关有效负载工厂的更多信息,请参见以下链接: https://docs.wso2.com/display/ESB490/PayloadFactory+Mediator

for more information about payload factory please see this link: https://docs.wso2.com/display/ESB490/PayloadFactory+Mediator

更新:

您必须使用开关盒或过滤器介体. 例如,这是一个开关案例.您必须完成正则表达式:

you must use switch case or filter mediator. for example this is a switch case sample. you must complete regex:

 <switch source="//body">
        <case regex="">
           <payloadFactory media-type="json">

           </payloadFactory>
        </case>
 </switch>

,或者您可以使用过滤器.在此示例中,过滤器匹配您给定的正则表达式或XPath.如果此评估返回true,则将发送true json.如果评估返回false,则将返回空json.

or you can use filter. In this example, the Filter match your given regular expression or XPath. If this evaluation returns true, it will send the true json. If the evaluation returns false, it will return empty json.

<filter (source="[XPath|json-eval(JSONPath)]" regex="string") | xpath="[XPath|json-eval(JSONPath)]">
    <then>
        <payloadFactory media-type="json">
            <format>{josn:"body"}</format>
            <args>your args<args/>
        </payloadFactory>
    </then>
    <else>
        <payloadFactory media-type="json">
            <format>{}</format>
            <args/>
        </payloadFactory>
    </else>
</filter>

此链接应为您提供帮助: https://docs.wso2.com/display/ESB490/Filter+Mediator

This link should help you: https://docs.wso2.com/display/ESB490/Filter+Mediator

这篇关于WSO2中XML到JSON的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆