播放2.1-RC2:从JSON读取密钥

时间:2013-01-14 12:20:30

标签: json scala playframework playframework-2.0 playframework-2.1

我尝试将我的网络应用程序从Play 2.0.4迁移到Play 2.1-RC2。

我的JSON数据包含未知密钥列表(key1key2),如下所示:

{description: "Blah",
 tags: [
   key1: ["value1", "value2"],
   key2: ["value3"]
 ]
}

我想将来自JSON的数据存储在元标记列表中。在Play 2.0.4中,我使用了类似这样的内容来阅读tags - 列表:

def readMetatags(meta: JsObject): List[Metatag] =
  meta.keys.toList.map(x => Metatag(x, (meta \ x).as[List[String]])

现在我想使用新的Play 2.1-JSON-API(原型):

import play.api.libs.json._
import play.api.libs.functional.syntax._

object Metatags {  
  implicit val metatagsRead: Read[Metatags] = (
    (JsPath \ "description").read[String] and
    (JsPath \ "tags").read[List[Metatag]]
  )(Metatags.apply _, unlift(Metatags.unapply _))

  implicit val metatagRead: Read[Metatag] = (
    JsPath().key. ?? read[String] and              // ?!? read key
    JsPath().values. ?? read[List[String]]         // ?!? read value list
  )(Metatag.apply _, unlift(Metatag.unapply _))

}

case class Metatags(description: String, tags: List[Metatag])
case class Metatag(key: String, data: List[String])

如何从JSON中读取密钥?

1 个答案:

答案 0 :(得分:5)

这是一个针对MetaTag类的自定义阅读器的解决方案。阅读只是将JsValue转换为JsObject,其中包含有用的fieldSet方法。

对于MetaTags,宏启动工作正常

object Application extends Controller {

  case class MetaTags(description: String, tags: List[MetaTag])
  case class MetaTag(key: String, data: List[String])

  implicit val readMetaTag = Reads(js => 
    JsSuccess(js.as[JsObject].fieldSet.map(tag => 
      MetaTag(tag._1, tag._2.as[List[String]])).toList))

  implicit val readMetaTags = Json.reads[MetaTags]

  def index = Action {
    val json = Json.obj(
      "description" -> "Hello world",
      "tags" -> Map(
        "key1" -> Seq("key1a", "key1b"),
        "key2" -> Seq("key2a"),
        "key3" -> Seq("Key3a", "key3b", "key3c")))

    val meta = json.as[MetaTags]
    Ok(meta.tags.map(_.data.mkString(",")).mkString("/"))
    // key1a,key1b/key2a/Key3a,key3b,key3c
  }

}