如何使用Play Framework(Scala)为Postgresql表中的json对象获取json响应输出?

时间:2016-02-18 10:22:48

标签: json postgresql scala playframework

我正在尝试使用Play框架(Scala)从Postgresql数据库返回json响应数据。我尝试了以下内容:但是我无法返回我的json响应输出。(因为我的数据库表中有几个json对象:jsondata,它有id和name)。我得到了:“type mismatch; found : anorm.SqlQuery required: String”。我不确定我的代码在哪里做错了从数据库(控制器和模型)获取json对象的json响应输出。请帮我解决这个问题,以获得输出并提前致谢。

controler:

import play.api.Play.current
import play.mvc.Controller;           
import play.libs.Json;
import play.libs.Json.*;                        
import com.fasterxml.jackson.databind.JsonNode;
import org.codehaus.jackson.JsonNode;           
import models.Getjsondata
import com.google.gson.Gson
import scala.util.{Try, Success, Failure}

 Getjsondata.getJsonValues(Getjsondata(Json.parse(id)), Json.parse(name));
 class MyController extends Controller {
 def getJson = Action { request =>
    println("calling getJson ... !!")//getting this message on play console
    val body: Anyname = request.body
    val textBody: Option[String] = body.asText
    val optionJson = textBody.flatMap(json => Try(Json.parse(json)).toOption)
    val bodyId = optionJson.map(_ \ "id")
    val bodyname = optionJson.map(_ \ "name")
    {
      for {
        id <- bodyId
        name <- bodyname
        json <- optionJson
      } yield {
        println("calling getJson else ... !!")
        val response = Getjsondata.getJsonValues(Getjsondata(id.as[Long],name.as[String]));
        Ok("Json data retrieved successfully: "+response)
      }
    } getOrElse BadRequest("Missing parameter either [id] or [name]")
  }
  }

模型:

import anorm._
import anorm.SqlParser._
import play.api.db._
import play.api.Play.current
import play.api.libs.json.{Json,JsValue}
import play.api.db.DB
import play.api.libs.json._
import anorm.~
import play.api.libs.functional.syntax._

case class Getjsondata (
  id: Pk[Long], name: String
)
object Getjsondata {
  val extractor = {
    get[Pk[Long]]("jsondata.id") ~
    get[String]("jsondata.name") map {
      case id~name => Getjsondata(id, name)
 }
 }
  def getJsonValues(jsondata: Getjsondata): String = {
  println("addJson getJsonValues page... !")
  DB.withConnection { implicit connection =>
 // SQL("select row_to_json(jsondata) from jsondata");
    SQL("select * from jsondata");//type mismatch; found : anorm.SqlQuery required: String
     }
     println("Test String... !") 
  }
}

1 个答案:

答案 0 :(得分:0)

您的Scala controller可能如下所示:

class MyController extends Controller {

  def getJson = Action { request =>
    println("calling getJson ... !!")

    val body: AnyContent = request.body
    val textBody: Option[String] = body.asText
    val optionJson = textBody.flatMap(json => Try(Json.parse(json)).toOption)

    val bodyId = optionJson.map(_ \ "id")
    val bodyName = optionJson.map(_ \ "name")

    {
      for {
        id <- bodyId
        name <- bodyName
        json <- optionJson
      } yield {
        println("calling getJson else ... !!")
        Getjsondata.getJsonValues(json)

        Ok("Json data retrieved successfully")
      }
    } getOrElse BadRequest("Missing parameter either [id] or [name]")
  }
}