Scala:将JSON直接解析为案例类 [英] Scala: Parse JSON directly into a case class

查看:256
本文介绍了Scala:将JSON直接解析为案例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个JSON字符串和一个与之对应的case类,将JSON解析为case类的简单方法是什么?有很多可用的库,但是似乎Scala现在可以立即使用.

Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box.

如果应该将JSON解析为case类的列表呢?

What about if the JSON should be parsed into a list of the case class?

更新:

Jerkson似乎被遗弃了,我不想安装完整的Play或Lift框架或其他任何笨重的东西.

Jerkson seems to be abandoned, and I don't want to install the full Play or Lift framework or anything else heavy.

推荐答案

有几个框架可以完全做到这一点.

There are several frameworks which can exactly do that.

圈出

现在经常使用.许多强大的功能.会把猫拉进来.

Used a lot nowdays. Many great features. Will pull cats in.

https://circe.github.io/circe/ https://github.com/circe/circe

JSON4s

JSON4s已经相当成熟,并且支持 jackson native JSON-Parser.在许多项目中都用它来代替jerkson.

JSON4s is quite mature and supports jackson or a native JSON-Parser. Used it in many projects to replace jerkson.

https://github.com/json4s/json4s

播放json

可以在没有完整播放堆栈的情况下使用.作为typesafe的play项目的一部分,提供了大力支持.

Can be used without the full play stack. Great support as part of the play project at typesafe.

http://www.playframework.com/documentation/2.0/ScalaJson

剥落皮

序列化框架.有一个选项可以序列化/反序列化为JSON.

A serialization Framework. There is an option to serialize/ and deserialize to JSON.

https://github.com/scala/pickling

喷涂JSON

可以进行序列化和反序列化.需要知道反序列化艰难的论点数量.

Can searialize and deserialize. Need to know number of arguments for deserialization tough.

https://github.com/spray/spray-json

这篇关于Scala:将JSON直接解析为案例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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