【发布时间】:2015-02-11 03:30:16
【问题描述】:
我的 json 在字段重复中具有相同的结构。
case class RuleJson(`type`: String, attribute: Int, operator: Option[String], value: String, is_value_processed: String, aggregator: String, conditions: RuleJson)
object RuleJson {
implicit val reads = Json.reads[RuleJson]
}
因此条件键将具有相同的 RuleJson 案例结构。 (虽然是可选的)
我收到“没有可用的 models.RuleJson 的隐式读取”。错误。
我的 JSON 是
{
"type": "salesrule/rule_condition_combine",
"attribute": null,
"operator": null,
"value": "1",
"is_value_processed": null,
"aggregator": "all",
"conditions": [
{
"type": "salesrule/rule_condition_product_subselect",
"attribute": "qty",
"operator": "==",
"value": "5",
"is_value_processed": null,
"aggregator": "all",
"conditions": [
{
"type": "salesrule/rule_condition_product",
"attribute": "quote_item_price",
"operator": "==",
"value": "200",
"is_value_processed": false
}
]
}
]
}
如果你看到条件字段重复,我该如何在 play scala 2.3 中验证这样的 JSON?
【问题讨论】:
-
恐怕你不能使用
Json.reads宏,因为递归类型的错误。您必须手动编写自己的读取。见:github.com/playframework/playframework/issues/2625 -
所以我无法解析一个递归的 Json 也不知道它有多少层?
-
您可以使用json combinators 构建
Reads -
谢谢你的帮助@Dimitri,你能看看stackoverflow.com/questions/27482488/…我需要你的帮助
标签: json scala playframework playframework-2.0