XStream 编组返回 xml 和 json 时出现问题
当我使用
new XStream().toXml(someObject);
它时,它返回以下 xml...
<response>
<status>SUCCESS</status>
<isOwnershipVerified class="boolean">false</isOwnershipVerified>
</response>
并且,当我使用
new XStream(new JsonHierarchicalStreamDriver()).toXml(someObject);
它时,它返回以下 json...
{"response": {
"status": "SUCCESS",
"isOwnershipVerified": {
"@class": "boolean""false"}
}}
现在,因为我想完全摆脱类属性(读取它不是为了将其与其他任何东西别名,而是将其删除)我使用以下代码。
XStream xStream = new XStream();
StringWriter writer = new StringWriter();
xStream.marshal(this, new PrettyPrintWriter(writer) {
@Override
public void addAttribute(final String key, final String value)
{
if (!key.equals("class"))
{
super.addAttribute(key, value);
}
}
});
return writer.toString();
它给出了以下 xml...
<response>
<status>SUCCESS</status>
<isOwnershipVerified>false</isOwnershipVerified>
</response>
但是,当我在上面创建 xStream 对象时传递 new JsonHierarchicalStreamDriver() 时,它不会返回 json。它返回与上面所示相同的 xml。
这是怎么回事?
提前致谢...
When i use
new XStream().toXml(someObject);
it returns following xml...
<response>
<status>SUCCESS</status>
<isOwnershipVerified class="boolean">false</isOwnershipVerified>
</response>
and, when i use
new XStream(new JsonHierarchicalStreamDriver()).toXml(someObject);
it returns following json...
{"response": {
"status": "SUCCESS",
"isOwnershipVerified": {
"@class": "boolean""false"}
}}
Now, since i want to get rid of class attribute altogether (read it not to alias it with anything else, but to remove it) i use following code.
XStream xStream = new XStream();
StringWriter writer = new StringWriter();
xStream.marshal(this, new PrettyPrintWriter(writer) {
@Override
public void addAttribute(final String key, final String value)
{
if (!key.equals("class"))
{
super.addAttribute(key, value);
}
}
});
return writer.toString();
which gives follwing xml...
<response>
<status>SUCCESS</status>
<isOwnershipVerified>false</isOwnershipVerified>
</response>
but, when i pass new JsonHierarchicalStreamDriver() while xStream object creation above, it does NOT return json. it returns the same xml shown above.
What is wrong going on here?
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了...
而不是使用
use
并且事情将开始按预期工作...:D
i figured it out...
instead of using
use
and the things will start working as expected... :D