Play-Json反序列化数据类型Map[X,List[Y]]
我有一个变量,Play-Json反序列化数据类型Map[X,List[Y]],json,scala,playframework-2.0,Json,Scala,Playframework 2.0,我有一个变量,Map[models.Account,List[models.Journal\u Account]]type 我不知道如何反序列化该数据类型。我通常使用以下代码: implicit val Account_detailWrites: Writes[Account] = ( (JsPath \ "account_code").write[String] and (JsPath \ "account_name").write[String] and (JsPat
Map[models.Account,List[models.Journal\u Account]]
type
我不知道如何反序列化该数据类型。我通常使用以下代码:
implicit val Account_detailWrites: Writes[Account] = (
(JsPath \ "account_code").write[String] and
(JsPath \ "account_name").write[String] and
(JsPath \ "account_type").write[Int] and
(JsPath \ "description").write[String] and
(JsPath \ "enable_payment").write[Boolean] and
(JsPath \ "expense_claims").write[Boolean]
)(unlift(Account.unapply))
implicit val Account_detailReads: Reads[Account] = (
(JsPath \ "account_code").read[String] and
(JsPath \ "account_name").read[String] and
(JsPath \ "account_type").read[Int] and
(JsPath \ "description").read[String] and
(JsPath \ "enable_payment").read[Boolean] and
(JsPath \ "expense_claims").read[Boolean]
)(Account.apply _)
因为它有列表[Y],我不知道该怎么办。。有人能帮我吗
更新:
谢谢你们的回答,但我已经用jerkson创建了一个Json响应。。
例如:
Ok(generate(variable_name)).as(application/json)
首先,您需要为您的模型定义读取和写入
e、 g
implicit val journalAccReads: Reads[JournalAccount] = ..
implicit val journalAccWrites: Writes[JournalAccount] = ...
然后
implicit val Account_detailReads: Reads[Account] = (
(JsPath \ "account_code").read[String] and
(JsPath \ "account_name").read[String] and
(JsPath \ "account_type").read[Int] and
(JsPath \ "description").read[String] and
(JsPath \ "enable_payment").read[Boolean] and
(JsPath \ "expense_claims").read[Boolean] and
(__ \ "journalAccounts").lazyRead(list[JournalAccount](journalAccReads)))
)(Account.apply _)
accountDetail写入的类似条目
顺便说一句,Scala/Java推荐驼峰式val
,var
名称 您的问题是Json对象具有String
键。游戏中的默认读取定义适用于Map[String,V]
,其中V
具有读取[V]
,而不是任何映射[K,V]
,即使K
具有读取[K]
您是否可以在映射中返回帐户名
,而不是整个帐户
?这在JSON中是什么样子?