如何将List
case class
作为response
投放。
我希望以json的形式给出回复,但无法获得如何将Case of Case Class转换为Json并将其作为回复传递给用户
控制器
def process = Action.async { implicit request =>
try{
val data= model1.searching("scala")
Future(Ok(Json.obj("result" -> "1", "data" ->??)))
} catch {
case e: Exception =>
Future(Ok(Json.obj("result" -> "0"))) //exception occured
}
}
模型
case class Team(name: String, image: String, nMember: BigInt, nYear: BigInt)
object model1{
def searching(term: String) = {
DB.withConnection { implicit c =>
val pinfo = SQL("SELECT name,image, no_member, no_year FROM table_name WHERE name like {term} limit 0,9 ").on("term" -> ("%" + term + "%"))()
var suggestions = List[Team]()
if (!(pinfo.isEmpty)) {
suggestions = pinfo.map {
row =>
Team(row[String]("name"), row[String]("image"), row[java.math.BigInteger]("no_member"), row[java.math.BigInteger]("no_year"))
}.toList
suggestions
}
}
}
使用"数据" - >数据 时出现 错误
[error] play - Cannot invoke the action, eventually got an error: scala.MatchError: ((result,success),(data,Team(publicBoard(The Best Car Of 2014 in India,28361273218382137,2014,198), Team(the best book,191919199199119,6534,12)))) (of class scala.Tuple2)
play.api.Application$$anon$1: Execution exception[[MatchError: ((result,success),(data,List(Team(The Best Car Of 2014 in India,28361273218382137,2014,198), Team(the best book,191919199199119,6534,12)))) (of class scala.Tuple2)]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.0]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
at scala.Option.map(Option.scala:145) [scala-library.jar:na]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.0]
Caused by: scala.MatchError: ((result,success),(data,List(Team(The Best Car Of 2014 in India,28361273218382137,2014,198), Team(the best book,191919199199119,6534,12)))) (of class scala.Tuple2)
at net.liftweb.json.Extraction$$anonfun$decompose$1.apply(Extraction.scala:82) ~[lift-json_2.10-2.5.1.jar:2.5.1]
at net.liftweb.json.Extraction$$anonfun$decompose$1.apply(Extraction.scala:82) ~[lift-json_2.10-2.5.1.jar:2.5.1]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.immutable.Map$Map1.foreach(Map.scala:109) ~[scala-library.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
答案 0 :(得分:2)
您需要为案例类定义序列化程序,然后转换将自动完成(通过含义)
package controllers
import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.Future
import scala.util.Try
object ProcessController extends Controller {
case class Team(tech: String, nMember: BigInt, Years: Int)
object Team {
implicit val bigIntFormat = new Format[BigInt] {
override def reads(json: JsValue): JsResult[BigInt] = json.validate[String].flatMap { s =>
Try(BigInt(s)).map(v => JsSuccess(v)).getOrElse(JsError(s"$s is not a number"))
}
override def writes(o: BigInt): JsValue = JsString(o.toString)
}
implicit val teamformat = Json.format[Team]
}
def process = Action.async { implicit request =>
val data = List(Team("scala", 12, 1), Team("java", 20, 5))
Future(Ok(Json.obj("result" -> "1", "data" -> data)))
}
}
我冒昧地使用Int而不是Integer来利用默认播放序列化器。
调用时,动作将生成以下json
{
"result": "1",
"data": [
{
"tech": "scala",
"nMember": "12",
"Years": 1
},
{
"tech": "java",
"nMember": "20",
"Years": 5
}
]
}