XStream解析没有根节点的JSON [英] XStream parse JSON with no root node

查看:496
本文介绍了XStream解析没有根节点的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用XStream对JSON进行反序列化,并且它一直运行良好。但是,当我有JSON字符串时,如下所示

I am currently deserializing JSON using XStream, and it has been working great. However, when I have JSON string like the following

{
    key1: { an_object: { something: 'foobar' } },
    key2: { another_object: { data: 'hi' }
}

最值得注意的是它没有根节点,我不知道如何解析它。基本上,我希望DROP_ROOT_NODE与反序列化相反。

most notably it doesn't have a root node, I'm not sure how to parse it. Basically, I want the opposite of DROP_ROOT_NODE for the deserialization.

推荐答案

简短的回答是你不能。

XStream需要知道要实例化的类,它从JSON(或XML)数据中获取知识。类名可以是别名,但不能省略。您可以通过以下方式解决:

XStream needs to know what class to instantiate, it gets that knowledge from JSON (or XML) data. Class name can be aliased, but it can not be omitted. You can work around by:


  1. 使用包含您的类名(或别名)的根节点手动包装JSON字符串

  2. 编写自己的阅读器,为您完成。但是,在这种情况下,您仍然需要将您的类名(别名)显式地或通过约定传递给该读取器(例如,总是先添加'root',然后在XStream实例中将其配置为您的类的别名) - 所以我不喜欢我认为这比#1更清洁。

这篇关于XStream解析没有根节点的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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