简体   繁体   English

如何在Play framework 2.3.x(Scala)中将case类转换为JSON?

[英]How to convert case class to JSON in Play framework 2.3.x (Scala)?

Anyone can show me how to convert case class class instances to JSON in Play framework (particularly Play v2.3.x) with Scala? 任何人都可以告诉我如何使用Scala将案例类类实例转换为Play框架(特别是Play v2.3.x)中的JSON?

For example I have code like this: 例如,我有这样的代码:

case class Foo(name: String, address: String)

def index = Action {
      request => {
        val foo = Foo("John Derp", "Jem Street 21")  // I want to convert this object to JSON
        Ok(Json.toJson(foo))    // I got error at here
      }
}

The error message: 错误消息:

Cannot write an instance of com.fasterxml.jackson.data bind.JsonNode to HTTP response. 无法将com.fasterxml.jackson.data bind.JsonNode的实例写入HTTP响应。 Try to define a Writeable[com.fasterxml.jackson.databind.JsonNode] 尝试定义一个可写的[com.fasterxml.jackson.databind.JsonNode]

UPDATE: I found out the above error is caused by wrong import of the Json class, it should be: import play.api.libs.json.Json . 更新:我发现上面的错误是由于错误导入Json类引起的,它应该是: import play.api.libs.json.Json However I still got error on implicit problem below. 但是我仍然在下面的隐含问题上有错误。

I have read this tutorial , but when I tried the implicit Writes[Foo] code: 我已经阅读了本教程 ,但是当我尝试隐式的Writes[Foo]代码时:

  implicit val fooWrites: Writes[Foo] = (
        (JsPath \ "name").write[String] and
            (JsPath \ "address").write[String]
        )(unlift(Foo.unapply))

I got Can't resolve symbol and and Can't resolve symbol unlift error in Intellij. 我得到Can't resolve symbol and Can't resolve symbol unlift中的Can't resolve symbol unlift错误。 Also the tutorial's code looks complex just for the conversion of an object to JSON. 此外,教程的代码看起来很复杂,只是为了将对象转换为JSON。 I wonder if there is more simple way to do this? 我想知道是否有更简单的方法来做到这一点?

You can get an Writes[Foo] instance by using Json.writes : 您可以使用Json.writes获取Writes[Foo]实例:

implicit val fooWrites = Json.writes[Foo]

Having this implicit in scope is all you need to convert Foo to JSON. 将此隐含在范围内是将Foo转换为JSON所需的全部内容。 See the documetnation here and here for more info about JSON reads/writes. 有关JSON读/写的更多信息,请参阅此处此处的文档。

The second problem - Can't resolve symbol and - is an Intellij bug introduced in version 1.3 of the Scala plugin. 第二个问题 - Can't resolve symbol and - 是Scala插件1.3版中引入的Intellij错误 In version 1.3.3 of the Scala plugin, there's now a workaround - set preference checkbox: 在Scala插件的1.3.3版本中,现在有一个解决方法 - 设置首选项复选框:

Languages & Frameworks > Scala > Core (default) tab > Use old implicit search algorithm 语言和框架> Scala> Core(默认)选项卡>使用旧的隐式搜索算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Play Framework 2.3.x(Scala)中为案例类定义交叉属性Json验证器 - Defining cross-attribute Json Validators for a case class in Play Framework 2.3.x (Scala) 播放2.3.x Scala - 如何在视图上显示json数据? - Play 2.3.x Scala - How to display json data on view? Play Framework-从版本2.3.x迁移到2.4.1:如何确定JSON值是否为JsUndefined - Play Framework - migration from version 2.3.x to 2.4.1: how to determine whether a JSON value is JsUndefined 播放2.3.x框架:如何拒绝所有非application / json请求 - play 2.3.x framework: How to decline all non application/json requests Play框架:从版本2.3.x迁移到2.4.1的问题 - Play Framework: Problems migrating from version 2.3.x to 2.4.1 Play 2.3 Scala - 如何在没有参考模态类的情况下验证 json - Play 2.3 Scala - How to validate json without a reference modal class 在grails 2.3.x中使用RestAPI JSON - Consuming a RestAPI JSON in grails 2.3.x Play 2.3.x中对Java8 ZonedDateTime的隐式json读写? - Implicit json Writes and Reads for Java8 ZonedDateTime in Play 2.3.x? 使用Scala / Play将JSON转换为带有嵌套对象的case类 - Convert JSON to case class with a nested objects using Scala/Play 在Scala Play框架中将json数组转换为模型类的Seq - Convert json array to Seq of model class in Scala Play framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM