杰森(Json)在Play 2.1.1中写作 [英] Json Writes in Play 2.1.1

查看:103
本文介绍了杰森(Json)在Play 2.1.1中写作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Playframework,并且正在使用Play 2.1.1和Slick 1.0.0来实现一个站点.我现在想把头放在Json Writes上,因为我想在我的一个控制器中返回Json.

I started using the Playframework recently and am implementing a site using Play 2.1.1 and Slick 1.0.0. I'm now trying to wrap my head around Json Writes as I want to return Json in one of my controllers.

我一直在寻找关于该主题的几处参考资料(例如此参考但无法弄清楚我在做什么错.

I've been looking at several references on the subject (like this one and this one but can't figure out what I'm doing wrong.

我有一个看起来像这样的模型:

I have a model looking like this:

case class AreaZipcode(  id: Int,
                     zipcode: String,
                     area: String,
                     city: String
                    )

object AreaZipcodes extends Table[AreaZipcode]("wijk_postcode") {

    implicit val areaZipcodeFormat = Json.format[AreaZipcode]

    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def zipcode = column[String]("postcode", O.NotNull)
    def area = column[String]("wijk", O.NotNull)
    def city = column[String]("plaats", O.NotNull)

    def autoInc = * returning id

    def * = id ~ zipcode ~ area ~ city <> (AreaZipcode.apply _, AreaZipcode.unapply _)
}

您可以看到我要使用的隐式val,但是当我尝试通过执行以下操作在控制器中返回Json时:

You can see the implicit val which I'm trying to use, but when I try to return the Json in my controller by doing this:

Ok(Json.toJson(areas.map(a => Json.toJson(a))))

我仍然仍然会遇到此错误消息:

I'm somehow still confronted with this errormessage:

No Json deserializer found for type models.AreaZipcode. Try to implement an implicit     Writes or Format for this type. 

我尝试了几种其他方式来实现Writes.例如,我尝试了以下方法,而不是上面的隐式val:

I've tried several other ways to implement the Writes. For instance, I've tried the following instead of the implicit val from above:

implicit object areaZipcodeFormat extends Format[AreaZipcode] {

    def writes(a: AreaZipcode): JsValue = {
      Json.obj(
        "id" -> JsObject(a.id),
        "zipcode" -> JsString(a.zipcode),
        "area" -> JsString(a.area),
        "city" -> JsString(a.city)
      )
    }
    def reads(json: JsValue): AreaZipcode = AreaZipcode(
      (json \ "id").as[Int],
      (json \ "zipcode").as[String],
      (json \ "area").as[String],
      (json \ "city").as[String]
    )

}

有人能指出我正确的方向吗?

Can someone please point me in the right direction?

推荐答案

JSON初始营救!您只需要写

import play.api.libs.json._
implicit val areaZipcodeFormat = Json.format[AreaZipcode]

就是这样.得益于Scala 2.10宏的神奇之处,不再需要编写自己的ReadsWrites了. (我建议您在使用JSON 上阅读Play的文档,它解释了很多)

That's it. No need to write your own Reads and Writes anymore, thanks to the magic of Scala 2.10 macros. (I recommend that you read Play's documentation on Working with JSON, it explains a lot.)

修改: 我没有注意到您在AreaZipcodes对象中已经有了Json.format.您需要将该行移出AreaZipcodes或将其导入到当前上下文中,即

I didn't notice you already had the Json.format inside the AreaZipcodes object. You either need to move that line out of AreaZipcodes or import it into your current context, i.e.

import AreaZipcodes.areaZipcodeFormat

这篇关于杰森(Json)在Play 2.1.1中写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆