简体   繁体   English

Play Framework 2 JSON读取,对一个变量进行反序列化

[英]Play Framework 2 JSON Reads, deserialization of one variable

I'm using Play Framework 2.4 and I'm trying to do a basic JSON deserialization with Reads but I get an error. 我正在使用Play Framework 2.4,并且正在尝试使用读取进行基本的JSON反序列化,但出现错误。 Here is the code: 这是代码:

case class Config(action: String)

and somewhere, 在某个地方

implicit val configReads: Reads[Config] = (
  (__ \ "action").read[String]
  )(Config.apply _)

I think the configReads is correctly formed but I get an IDE error on the "read" method call (symbol not defined), when I compile the code I get the following error: 我认为configReads的格式正确,但是在“ read”方法调用(未定义符号)上出现IDE错误,在编译代码时出现以下错误:

Error:(30, 27) overloaded method value read with alternatives:
  (t: String)play.api.libs.json.Reads[String] <and>
  (implicit r: play.api.libs.json.Reads[String])play.api.libs.json.Reads[String]
 cannot be applied to (String => wings.common.json.Config)
      (__ \ "action").read[String]
                          ^

but, if instead of trying to deserialize ONE argument I declare a class with TWO arguments in the constructor and I write the code to deserialize it, it works. 但是,如果不是尝试反序列化一个参数,而是在构造函数中声明一个带有两个参数的类,然后编写代码对其反序列化,则它可以工作。

Does anybody know how to solve this? 有人知道如何解决这个问题吗?

Edit: 编辑:

Digging in the depth of Google I found this for Play 2.1.x but I'm using the Json library for Play 2.4.1 so this problem should not be happening. 深入研究Google时,我发现适用于Play 2.1.x,但是我将Json库用于Play 2.4.1,因此应该不会发生此问题。

You can do like this: 您可以这样:

implicit val configReads: Reads[Config] = (
  (__ \ "action").read[String]
  ) map Config.apply

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM