Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JSON列表到Scala列表_Json_Scala_Playframework_Anorm - Fatal编程技术网

从JSON列表到Scala列表

从JSON列表到Scala列表,json,scala,playframework,anorm,Json,Scala,Playframework,Anorm,我正在用Play框架和Scala创建一个后端API。我想将传入的请求映射到scala对象。对象的实例变量之一是通道列表。以下是我目前拥有的: 接受请求并尝试将其映射到用户的控制器方法: def addUser = Action(parse.json) { request => request.body.validate[User].fold({ errors => BadRequest(Json.obj( "status" -> "Error",

我正在用Play框架和Scala创建一个后端API。我想将传入的请求映射到scala对象。对象的实例变量之一是通道列表。以下是我目前拥有的:

接受请求并尝试将其映射到用户的控制器方法:

def addUser = Action(parse.json) { request =>
  request.body.validate[User].fold({ errors =>
    BadRequest(Json.obj(
      "status" -> "Error",
      "message" -> "Bad JSON",
      "details" -> JsError.toFlatJson(errors)
    ))
  }, { user =>
    User.create(user.pushToken, user.channels)
    Ok(Json.obj("status" -> "OK", "message" -> "User created"))
  })
}
用户案例类:

case class User(id: Pk[Long], pushToken: String, channels: List[String])
用户格式化程序:

implicit val userFormat = (
  (__ \ "id").formatNullable[Long] and
  (__ \ "pushToken").format[String] and
  (__ \ "channels").format[List[String]]
  )((id, pushToken, channels) => User(id.map(Id(_)).getOrElse(NotAssigned), pushToken, channels),
  (u: User) => (u.id.toOption, u.pushToken, u.channels))
用户名称创建方法:

def create(pushToken: String, channels: List[String]) {
  DB.withConnection { implicit c =>
    SQL("insert into user (pushToken, channels) values ({pushToken}, {channels})").on(
      'pushToken -> pushToken,
      'channels -> channels
    ).executeUpdate()
  }
}
当我尝试编译时,我得到:

Compilation error[could not find implicit value for parameter extractor: anorm.Column[List[String]]]
理想情况下,我希望作为一个用户能够接受:

{
  "pushToken":"4jkf-fdsja93-fjdska34",
  "channels": [
    "channelA", "channelB", "channelC"
  ]
}

然后从中创建一个用户。

在Anorm中不能使用List[String]作为列值,这就是问题所在


您应该使用mkString方法或smth-else

哪个DBMS?您的模式是什么样子的?错误信息是说anorm不知道如何将列表映射到数据库字段,我想。@RobinGreen我还处于开发过程的早期,正在使用内存中的默认数据库。我想这也是我问题的一部分。我是否必须有一个不同的数据库表将频道映射到用户?在重头戏1中,我可以将数据库字段设置为字节数组,JPA将为我映射其余字段。是的,我会说您需要一个单独的表来进行通道到用户的映射。如果要将其存储在字节数组中,则可能必须更改
(\uuu \“通道”).format[List[String]]