Axis2 JSon支持(Jettison)错误的xml转换 [英] Axis2 JSon Support (Jettison) bad xml conversion

查看:147
本文介绍了Axis2 JSon支持(Jettison)错误的xml转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Axis2 1.6.4来实现REST Json WebServices( https://axis.apache.org/axis2/java/core/docs/json_support.html ),当Jettison将Json对象转换为XML(如果它没有"root"元素)时,我会遇到一个问题.详细信息:

I am using Axis2 1.6.4 to implement REST Json WebServices (https://axis.apache.org/axis2/java/core/docs/json_support.html) and I face an issue when Jettison converts Json object To XML if it does not have a "root" element. Details:

如果请求是:

{"name":"John","age":30}

然后,服务器端的XML OMElement是:

Then XML OMElement at server side is:

<name>John</name

因此缺少年龄元素

相反,如果请求是:

{person:{"name":"John","age":30}}

然后,服务器端的XML OMElement是:

Then XML OMElement at server side is:

<person><name>John</name><age>30</age></person>

感谢您的帮助, 马丁(Martí)

Thanks for your help, Martí

推荐答案

我已经找到解决此JSON-> XML转换问题的方法.感谢这篇文章:如何使用Axis2 JSON ,我意识到我能够在将输入json转换为XML之前进行访问,并且还可以从json字符串创建OMElement.因此,当输入是json请求时,我用json"root"元素包装它,然后将其转换为XML而不会丢失数据. 如果对某人有用,则下面是获取输入json字符串的源代码和将json字符串转换为OMElement的源代码

I've founda work around to overcome this JSON->XML conversion issue. Thanks to this post:How to use Axis2 JSON, I've realized I am able to access input json before converting it to XML, and also to create an OMElement from a json string. So when input is a json request, I wrap it with a json "root" element, and then convert it to XML without losing data. If it could be useful to someone, below is the source code to get input json string and the source code to convert json string to OMElement

获取输入的json字符串并包装成根

    OMSourcedElement source=(OMSourcedElement )msg;
    AbstractJSONDataSource jsonSoucre=(AbstractJSONDataSource)source.getDataSource();

    MessageContext msgCtxt= MessageContext.getCurrentMessageContext();

    JSONDataSource jsonRequestEnvDS= new JSONDataSource(new StringReader("{\"JSONEnvelope\": " + jsonSoucre.getObject() + " }"), msgCtxt);
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMSourcedElement omRequest =  factory.createOMElement(jsonRequestEnvDS,null,null);

从json字符串创建OMElement

    String jsonString="{...}";
    JSONDataSource jsonDS= new JSONDataSource(new StringReader(jsonString),msgCtxt);
    factory = OMAbstractFactory.getOMFactory();
    OMSourcedElement resonseJson =  factory.createOMElement(jsonDS,null,null);

    return  resonseJson;

这篇关于Axis2 JSon支持(Jettison)错误的xml转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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