Play Framework:如何过滤 json 的值?
Play Framework: how to filter values of a json?
我想在属性字段中过滤我的 json。
json是这样的:
"subject": "cars",
"price": [25],
"location":{city: "Oslo", postalcode :441},
"attributes" :
[{"key":"tc", "value":2, "key_label":typecars", "value_label":"WPD"},
{"key":"lk", "value":1, "key_label":"lookupcars", "valnotEqualReadsue_label":"fed"}
{"key":, "value":6, "key_label":"year", "value_label":"fzef"}
{"key":"nc", "value":3, "key_label":ncars", "value_label":"POD"}
{"key":"tc", "value":4, "key_label":plcars", "value_label":"LOD"}
{"key":"pm", "value":5, "key_label":pomcars", "value_label":"PLD"}]
我只想保留 key_label 和 value_label,其中 key_label ="year" 和 "pomcars"。
我该怎么做?
我试过了
case class Attribut(key_label: Option[String]
, value_label: Option[String]
)
case class Ads(subject: Option[String]
, price: Option[Int]
, location: Option[Location]
, attribut: Option[Seq[Attribut]]
)
implicit val adsReads: Reads[Ads] = (
(JsPath \ "subject").readNullable[String] and
(JsPath \ "price" \ 0).readNullable[Int] and
(JsPath \ "location").readNullable[Location] and
(JsPath \ "attributes").readNullable[Seq[Attribut]].filter {
case Some(Attribut(Some(key_label), _)) => (key_label == "year") || key_label == "pomcars")
case _ => false
}
) (Ads.apply _)
我遇到以下错误:无法将构造函数实例化为预期类型;
发现:属性
要求:Seq[属性]
感谢您的帮助。
最简单的方法是只读入 JSON 并在它是 类.
的序列时处理它
比如这样读入:
case class Attribute(key_label: Option[String], value_label: Option[String])
object Attribute {
implicit val reads: Reads[Attribute] = Json.reads[Attribute]
}
json.as[Seq[Attribute]] // or `.asOpt` if you can't guarantee that it'll work
然后你可以做一些事情,比如过滤掉你不想要的值或折叠 Seq,例如:
json.as[Seq[Attribute]].filter {
case Attribute(Some(key_label), _) => (key_label == "year") || (key_label == "pomcars")
case _ => false
}
或:
json.as[Seq[Attribute]].foldLeft(Seq[Attribute]()) {
case (acc, curr@Attribute(Some(key_label), _))
if (key_label == "year") || (key_label == "pomcars") => acc :+ curr
case (acc, _) => acc
}
这两个结果都是 res0: Seq[Attribute] = List(Attribute(Some(year),Some(fzef)), Attribute(Some(pomcars),Some(PLD)))
。
我想在属性字段中过滤我的 json。
json是这样的:
"subject": "cars",
"price": [25],
"location":{city: "Oslo", postalcode :441},
"attributes" :
[{"key":"tc", "value":2, "key_label":typecars", "value_label":"WPD"},
{"key":"lk", "value":1, "key_label":"lookupcars", "valnotEqualReadsue_label":"fed"}
{"key":, "value":6, "key_label":"year", "value_label":"fzef"}
{"key":"nc", "value":3, "key_label":ncars", "value_label":"POD"}
{"key":"tc", "value":4, "key_label":plcars", "value_label":"LOD"}
{"key":"pm", "value":5, "key_label":pomcars", "value_label":"PLD"}]
我只想保留 key_label 和 value_label,其中 key_label ="year" 和 "pomcars"。 我该怎么做?
我试过了
case class Attribut(key_label: Option[String]
, value_label: Option[String]
)
case class Ads(subject: Option[String]
, price: Option[Int]
, location: Option[Location]
, attribut: Option[Seq[Attribut]]
)
implicit val adsReads: Reads[Ads] = (
(JsPath \ "subject").readNullable[String] and
(JsPath \ "price" \ 0).readNullable[Int] and
(JsPath \ "location").readNullable[Location] and
(JsPath \ "attributes").readNullable[Seq[Attribut]].filter {
case Some(Attribut(Some(key_label), _)) => (key_label == "year") || key_label == "pomcars")
case _ => false
}
) (Ads.apply _)
我遇到以下错误:无法将构造函数实例化为预期类型; 发现:属性 要求:Seq[属性]
感谢您的帮助。
最简单的方法是只读入 JSON 并在它是 类.
的序列时处理它比如这样读入:
case class Attribute(key_label: Option[String], value_label: Option[String])
object Attribute {
implicit val reads: Reads[Attribute] = Json.reads[Attribute]
}
json.as[Seq[Attribute]] // or `.asOpt` if you can't guarantee that it'll work
然后你可以做一些事情,比如过滤掉你不想要的值或折叠 Seq,例如:
json.as[Seq[Attribute]].filter {
case Attribute(Some(key_label), _) => (key_label == "year") || (key_label == "pomcars")
case _ => false
}
或:
json.as[Seq[Attribute]].foldLeft(Seq[Attribute]()) {
case (acc, curr@Attribute(Some(key_label), _))
if (key_label == "year") || (key_label == "pomcars") => acc :+ curr
case (acc, _) => acc
}
这两个结果都是 res0: Seq[Attribute] = List(Attribute(Some(year),Some(fzef)), Attribute(Some(pomcars),Some(PLD)))
。