Jackson Scala 模块、嵌套列表和映射
我希望转换 json 对象列表,这些对象本身可能包含 Json 对象或 Json 对象列表,并且希望结果是适当的 Scala 映射或列表,但是,我只能将顶级对象转换为地图或列表。 有没有一种简单的方法可以做到这一点?
例如在 REPL
objectMapper.readValue("{\"test\":\"113123\",\"myList\":[{\"test2\":\"321323\"},{\"test3\":\"11122\"}]}", classOf[Map[String,Any]])
中将返回
res: Map[String,Any] = Map(test -> 113123, myList -> [{test2=321323}, {test3=11122}])
我想要的位置
res: Map[String,Any] = Map(test -> 113123, myList -> List(Map(test2 -> 321323), Map(test3 -> 111222)))
I wish to convert lists of json objects, which in themselves may contain Json objects, or Lists of Json objects and would like the results to be Scala Maps or Lists as appropriate, however, I can only get the top level object to convert to a Map or a List.
Is there a simple way to do this?
For example in REPL
objectMapper.readValue("{\"test\":\"113123\",\"myList\":[{\"test2\":\"321323\"},{\"test3\":\"11122\"}]}", classOf[Map[String,Any]])
Will return
res: Map[String,Any] = Map(test -> 113123, myList -> [{test2=321323}, {test3=11122}])
Where I would like
res: Map[String,Any] = Map(test -> 113123, myList -> List(Map(test2 -> 321323), Map(test3 -> 111222)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,所以使用更高版本的 scala jackson 模块我创建了这个类
,您可以将其应用于对象映射器
OK so using a later version of the scala jackson module I created this class
which you can apply to the object mapper
如果这不能按预期工作,请在 [https://github.com/FasterXML/jackson-module-scala/issues] 上针对 Jackson Scala 模块提出问题。在“无类型”模式(即非 bean,使用列表、映射等)处理内容时可能会出现问题,scala 模块需要为其添加特殊处理。潜在的挑战是 Scala 集合通常不能与 JDK 集合完全映射。
If this does not work as expected, please file an issue against Jackson Scala Module at [https://github.com/FasterXML/jackson-module-scala/issues]. It is possible that there are issues with handling of contents in "untyped" mode (i.e. non-bean, using Lists, Maps etc) which scala module needs to add special handling for. The underlying challenge is that Scala collections in general do not map cleanly with JDK collections.
我没有使用过 Jackson Scala 模块,但 Jerkson 中的行为是相同的,所以我会冒险猜测发生了什么。当 REPL 打印时:
它告诉您有一个包含“myList”的元组和一个 java.util.ArrayList。 ArrayList 包含两个 java.util.LinkedHashMap。 (我解释这一点是因为一开始这对我来说并不明显,我不得不花一些时间使用 getClass 和 asInstanceOf 来研究 REPL 来深入研究该结构。)
我相当确定没有办法获得 Jackson Scala 模块或 Jerkson 返回带有本机 Scala 集合的反序列化 JSON。您可以导入 scala.collect.JavaConversions 到让这些值看起来更像 Scala 一点。即使你这样做了,你仍然需要改变一些事情。例如:
如果您不想摆弄所有的转换,我认为您需要做的是定义一个要反序列化的类。例如,使用 Jerkson:(
我将其写为完整程序的原因是 Jerkson 不处理 REPL 中的反序列化案例类。)
我知道您可以使用 Jackson 反序列化为对象,但上次我查看它花了再走几步。
这是回答您的问题的正确途径吗?
I've not used Jackson Scala Module, but the behavior is the same in Jerkson, so I'll hazard a guess as to what's going on. When the REPL is printing:
it's telling you that there's a tuple containing "myList" and an java.util.ArrayList. The ArrayList contains two java.util.LinkedHashMaps. (I explain this because it wasn't obvious to me at first and I had to spend some quality time with the REPL using getClass and asInstanceOf to dive into that structure.)
I'm fairly certain there's not a way to get Jackson Scala Module or Jerkson to return deserialized JSON with native Scala collections. You can import scala.collect.JavaConversions to make the those values appear a little more Scala-like. Even if you do that, you'll still have to cast things around. For example:
If you don't want to have to fiddle with all of the casting, I think what you need to do is define a class to deserialize to. So for example, using Jerkson:
(The reason I wrote that as a full program is that Jerkson doesn't handle deserializing case classes in the REPL.)
I know you can deserialize to an object with Jackson, but last time I looked it took a few more steps.
Is this on the right track to answering your question?
最终我听从了Leif的建议,编写了这段代码来解决问题
Ultimately I followed Leif's advice and wrote this code to solve the problem