0

我想通过应用转换器来更新 jsValue 中的所有对象

import play.api.libs.json._

val json = Json.obj(
"whatever" -> 1,
"content" -> "hello world",
"sub" -> Json.obj("content" -> "love world"
))

val transformer = (__ \\ 'content).json.update(
  __.readNullable[JsString].map{
    case Some(JsString(str)) => JsString(str.replaceAll("world", "scala"))
  }
)

json.transform(transformer)

我希望看到这个结果:

Json.obj(
"whatever" -> 1,
"content" -> "hello scala",
"sub" -> Json.obj("content" -> "love scala")
)

我得到错误:

[error] java.lang.RuntimeException: expected KeyPathNode (JsPath.scala:138)
[error] play.api.libs.json.JsPath$.step$1(JsPath.scala:138)
[error] play.api.libs.json.JsPath$.play$api$libs$json$JsPath$$buildSubPath$1(JsPath.scala:147)
[error] play.api.libs.json.JsPath$$anonfun$createObj$1.apply(JsPath.scala:152)
[error] play.api.libs.json.JsPath$$anonfun$createObj$1.apply(JsPath.scala:150)
[error] play.api.libs.json.JsPath$.createObj(JsPath.scala:150)

但似乎递归路径(反斜杠)不起作用,有可能做到这一点吗?

4

0 回答 0