JSON读/写错误:应用程序不接受参数
我正在尝试为一个case类编写一个读/写值,其中许多值可能为null。根据Play框架的教程,我将其编写为JSON读/写错误:应用程序不接受参数,json,scala,playframework-2.0,Json,Scala,Playframework 2.0,我正在尝试为一个case类编写一个读/写值,其中许多值可能为null。根据Play框架的教程,我将其编写为 implicit val incomingMessageWrites: Writes[IncomingMessage] = ( (JsPath \ "stageNum").write[Int] and (JsPath \ "stage").write[String] and (JsPath \ "stageTime").write[Long]
implicit val incomingMessageWrites: Writes[IncomingMessage] = (
(JsPath \ "stageNum").write[Int] and
(JsPath \ "stage").write[String] and
(JsPath \ "stageTime").write[Long] and
(JsPath \ "vendorId").write[String] and
(JsPath \ "text").writeNullable[String] and
(JsPath \ "campaignCode").writeNullable[String] and
(JsPath \ "osTotals").writeNullable[OSTotals] and
(JsPath \ "splitSegment").writeNullable[Int] and
(JsPath \ "segmentNum").writeNullable[Int] and
(JsPath \ "osBatchStatus").writeNullable[OSBatchStatus]
)(unlift(IncomingMessage.unapply))
然而,我一直遇到同样的问题,IntelliJ告诉我提升方法不起作用,因为“应用程序不接受参数”
这方面的case类是
case class IncomingMessage(
stageNum: Int,
stage: String,
stageTime: Long,
vendorId: String,
text: String,
campaignCode: String,
osTotals: OSTotals,
splitSegment: Int,
segmentNum: Int,
osBatchStatus: OSBatchStatus) {
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, 0, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, splitSegment: Int, vendorId: String, text: String, campaignCode: String) =
this(stageNum, stage, stageTime, vendorId, text, campaignCode, null, splitSegment, 0, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, segmentNum: Int, vendorId: String, osTotals: OSTotals) =
this(stageNum, stage, stageTime, vendorId, null, null, osTotals, 0, segmentNum, null)
def this(stageNum: Int, stage: String, stageTime: Long, vendorId: String, oSBatchStatus: OSBatchStatus) =
this(stageNum, stage, stageTime, vendorId, null, null, null, 0, 0, osBatchStatus)
}
另一个case类也会发生同样的情况
case class OSBatchStatus(os: String, success: Int, failure: Int)
object OSBatchStatus {
implicit val oSBatchStatusReads: Reads[OSBatchStatus] = (
(JsPath \ "os").readNullable[String] and
(JsPath \ "success").readNullable[Int] and
(JsPath \ "failure").readNullable[Int]
)(OSBatchStatus.apply _)
}
我编写了读写值,但为了节省空间,省略了它们
我们将非常感谢您的任何帮助 如果输入JSON文件中有可选字段,那么案例类的相应字段也应该是可选的。您对
的定义为OSBatchStatus
的定义假定所有字段都是可选的,因此您的case类应该如下所示:
case class OSBatchStatus(os: Option[String], success: Option[Int], failure: Option[Int])
同样的规则也适用于您的IncomingMessage
案例类(四个字段是必需的,每隔一个字段应该是选项[T]
)。此外,由于JSON文件中的字段与case类中的字段具有完全相同的名称,因此可以使用play JSON方法为您自动生成Reads/Writes/Format
:
import play.api.libs.functional.syntax._
import play.api.libs.json._
implicit val incomingMessageWrites = Json.writes[IncomingMessage]
implicit val osBatchStatusReads = Json.reads[OSBatchStatus]
implicit val osTotalsFormat = Json.format[OSTotals] // Will generate both `Reads` and `Writes`
这解决了我的问题。非常感谢。