【发布时间】:2016-02-16 07:16:34
【问题描述】:
我的示例 json 带有国家对象 Json 示例 1
"@version": "1.0",
"country": {
"@country": "US",
"day": {
"@date": "2016-02-15",
"@value": "1"
}
}
或使用国家/地区数组: Json 示例 2
"@version": "1.0",
"country": [{
"@country": "US",
"day": {
"@date": "2016-02-15",
"@value": "1"
}
}, {
"@country": "UK",
"day": {
"@date": "2016-02-15",
"@value": "5"
}]
}
读取 json
implicit val dayJsonReads: Reads[DayJson] = (
(JsPath \ "@date").read[DateTime](dateReads) and
((JsPath \ "@value").read[Int] orElse (JsPath \ "@value").read[String].map(_.toInt))
)(DayJson.apply _)
implicit val countryJsonReads: Reads[CountryJson] = (
(JsPath \ "@country").read[String] and
(JsPath \ "day").read[DayJson]
)(CountryJson.apply _)
implicit val newUserJsonReads: Reads[NewUserJson] = (
(JsPath \ "@version").read[String] and
(JsPath \ "country").readNullable[Seq[CountryJson]]
)(NewUserJsonParent.apply _)
上面的代码读取示例 json 2,但示例 json 1 失败。是否可以使用 readNullable 读取 JS 值或 JS 对象,或者我们可以将其从 JS 值转换为 JS 对象。谢谢。
【问题讨论】:
标签: json scala playframework-2.4