scala request.body.asJson 访问数组
scala request.body.asJson access Array
在我的 play Framework 应用程序中,我向后端发送了一个 json,在后端,我想访问 json
中的数组
我试过这个:
val processSteps = request.body.asJson.map{
json => (json \ "stepsData").asOpt[Object].map { steps =>
println(steps)
}
for (processStep <- steps) {
processStep.map(_.validate[ProcessStepTemplatesModel] match {
stepsData 是我要访问的数组
要求:
Some({"steps":"","stepsData":[{"steptitle":"sd","title":"asd"}],"stepType":"duty","createdat":"2017-05-31 14:30:26","updatedat":"2017-05-31 14:30:26","activeSnackbar":false,"snackbarText":{"type":"span","key":null,"ref":null,"props":{"children":[{"key":null,"re
f":null,"props":{"value":"snackbar.processes.createProcess"},"_owner":null,"_store":{}},": ",null]},"_owner":null,"_store":{}},"approvedProcessTemplates":[],"approveprocess":17,"approveprocessTitle":"Felix","trainingsprocess":47,"trainprocessTitle":"Posaune","d
eleted":false,"dialogActive":false,"title":"ads","processtemplate":1,"loaded":true,"version":"asd","responsible":"asd","accountable":"asd","consulted":"asd","informed":"asd","deadline":"2017-05-18T12:30:09.460Z","dialogActions":[{"label":{"key":null,"ref":null,
"props":{"value":"dialog.processes.deleteProcess.cancel"},"_owner":null,"_store":{}}}],"dialogTitle":{"key":null,"ref":null,"props":{"value":"dialog.processes.selectProcessTemplate.trainingsprocess.header"},"_owner":null,"_store":{}},"dialogHandlerVariable":1})
您应该在 json 验证中执行此操作,即在您的 asOpt
方法中:
(json \ "stepsData").asOpt[Array[ProcessStepTemplatesModel]]
如果您在正文中使用其他字段,您甚至可以为预期的正文定义一个案例 class,并直接验证它:
case class StepProcessingRequestData(
stepsData: Array[ProcessStempTemplatesModel],
... // add your other fields, as defined in your JSON schema
)
object StepProcessingRequestData {
implicit val reads = Json.reads[StepProcessingRequestData]
}
val processStepsOpt: Option[StepProcessingRequestData] =
request.body.asJson.flatMap(_.asOpt[StepProcessingRequestData])
你能告诉我你的数据是什么样子的吗?
我处理这个问题的方法是,有一个案例 class for stepsData :
case class StepData(attributes ..)
然后有一个包装器:
case class StepDataWrapper(stepsData : List[StepData])
然后在您的控制器中指定隐式格式:
import play.api.libs.json._
implicit val stepDataFormat = Json.format[StepData]
implicit val stepDataListWrapperFormat =Json.format[StepDataWrapper]
然后,
val processSteps = request.body.asJson.asOpt[StepDataWrapper]
.map{
stepDataWrapper => stepDataWrapper.stepsData.map(println(_))
}
在我的 play Framework 应用程序中,我向后端发送了一个 json,在后端,我想访问 json
中的数组我试过这个:
val processSteps = request.body.asJson.map{
json => (json \ "stepsData").asOpt[Object].map { steps =>
println(steps)
}
for (processStep <- steps) {
processStep.map(_.validate[ProcessStepTemplatesModel] match {
stepsData 是我要访问的数组
要求:
Some({"steps":"","stepsData":[{"steptitle":"sd","title":"asd"}],"stepType":"duty","createdat":"2017-05-31 14:30:26","updatedat":"2017-05-31 14:30:26","activeSnackbar":false,"snackbarText":{"type":"span","key":null,"ref":null,"props":{"children":[{"key":null,"re
f":null,"props":{"value":"snackbar.processes.createProcess"},"_owner":null,"_store":{}},": ",null]},"_owner":null,"_store":{}},"approvedProcessTemplates":[],"approveprocess":17,"approveprocessTitle":"Felix","trainingsprocess":47,"trainprocessTitle":"Posaune","d
eleted":false,"dialogActive":false,"title":"ads","processtemplate":1,"loaded":true,"version":"asd","responsible":"asd","accountable":"asd","consulted":"asd","informed":"asd","deadline":"2017-05-18T12:30:09.460Z","dialogActions":[{"label":{"key":null,"ref":null,
"props":{"value":"dialog.processes.deleteProcess.cancel"},"_owner":null,"_store":{}}}],"dialogTitle":{"key":null,"ref":null,"props":{"value":"dialog.processes.selectProcessTemplate.trainingsprocess.header"},"_owner":null,"_store":{}},"dialogHandlerVariable":1})
您应该在 json 验证中执行此操作,即在您的 asOpt
方法中:
(json \ "stepsData").asOpt[Array[ProcessStepTemplatesModel]]
如果您在正文中使用其他字段,您甚至可以为预期的正文定义一个案例 class,并直接验证它:
case class StepProcessingRequestData(
stepsData: Array[ProcessStempTemplatesModel],
... // add your other fields, as defined in your JSON schema
)
object StepProcessingRequestData {
implicit val reads = Json.reads[StepProcessingRequestData]
}
val processStepsOpt: Option[StepProcessingRequestData] =
request.body.asJson.flatMap(_.asOpt[StepProcessingRequestData])
你能告诉我你的数据是什么样子的吗?
我处理这个问题的方法是,有一个案例 class for stepsData :
case class StepData(attributes ..)
然后有一个包装器:
case class StepDataWrapper(stepsData : List[StepData])
然后在您的控制器中指定隐式格式:
import play.api.libs.json._
implicit val stepDataFormat = Json.format[StepData]
implicit val stepDataListWrapperFormat =Json.format[StepDataWrapper]
然后,
val processSteps = request.body.asJson.asOpt[StepDataWrapper]
.map{
stepDataWrapper => stepDataWrapper.stepsData.map(println(_))
}