是否有一个 Java JSON 反序列化器可以将字符串解码为列表字典或原始类型字典列表
我需要从数据库中提取不基于标准对象的 JSON 文档。
有没有办法使用JAVA将这些文档“反序列化”为列表和列表?原始对象的字典(字符串、整数、布尔值等)
有什么库可以在两个方向上执行此操作? 换句话说,我正在寻找 System.Runtime.Serialization.Json 的 Java 计数器部分
I need to pull back from a database JSON documents that are not based on a standard object.
Is there a way using JAVA to "deserialize" these documents into Lists & Dictionaries of primitive objects (string, int, bool, etc...)
Any library that can do this in both directions?
In other words I am looking for the Java counter part of System.Runtime.Serialization.Json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议不要尝试将 JSON 强制转换为难以使用的
Map
或List
使用这些对象构建 JSON 树也很容易。
Rather than trying to force JSON into a difficult to use
Map<String, Object>
orList<Object>
, I'd recommend a library that can parse JSON into a tree model. While many libraries can do this sort of thing, Google Gson is my favorite. You can use itsJsonParser
class to parse JSON as a tree starting with a root JsonElement. Once you have theJsonElement
, it should be considerably easier to use than someObject
that might be aList
and might be aMap
.It's easy to build a JSON tree using these objects as well.
几乎 http://json.org/ 上的每个 Java 库都可以做到这一点,您实际上尝试过找到一些东西吗?
例如,对于 Jackson,您会这样做:
但创建自己的要映射到的 Java 对象通常很方便;无需使用标准对象。例如:
有关更多示例,您可以查看Jackson 教程。
Almost every single Java library at http://json.org/ can do this, did you actually try finding something?
For example, with Jackson you would do:
but it is often convenient to create your own Java objects to map to; there is no need to use standard objects. For example:
For more examples, you can check out Jackson tutorial.
Flexjson 绝对可以做到。下面的行将 jsonString 反序列化为 HashMap
Flexjson definitely does it. The below line de-serialize a jsonString to a HashMap
抱歉,JSON 没有任何有关数据类型的元信息,因此除了字符串列表之外,没有机会将任何不基于标准对象的内容转换回来。
更新:我可以考虑使用 Groovy 来使用未分类的 JSON 字符串制作 POGO。然后您还可以使用动态对象。也许您还可以利用 http://json-lib.sourceforge.net/。
Sorry but JSON does not have any metainformation about datatypes, thus there is no chance to convert anything back not based on standard objects, except a list of strings.
UPDATE: I could think of making POGOs with Groovy out of a not classified JSON-String. Then you can also work with dynamic Objects. Maybe you can also utilize http://json-lib.sourceforge.net/.