1

如何使用基于兄弟属性的 scala lift 转换下面的 json?在下面的json中,如果兄弟属性“type”是“html”,我想对“value”属性中的值进行编码

val json = """
{
    "id" : "1B23423B",
    "payload" : {
        "list" : [ {
            "name" : "test",
            "data" : [ {
                "value" : "Some html",
                "type" : "html",
            }, {
                "value" : "some text",
                "type" : "text"
            }]
         }]
    }
}
"""
def encode(s:String):String = s + "encoded"
val newjson = js.transform { 
    case x if x == JField("type",JString("html")) => // somehow encode value??
}

println(newjson)
4

1 回答 1

3

以下是可能的解决方案:

1)首先找到html类型的数据json

2)转换json值child

  val parsed = JsonParser.parse(jsonString)  

  def encode(s:String):String = s + "encoded"

  private def encodeValue(dataObject: JValue) = dataObject.transform{
    case JField("value", JString(value)) => JField("value", JString(encode(value)))
  }

  val newJson = parsed.transform({
    case dataObject @ JObject(fields) if fields.contains(JField("type", JString("html"))) => encodeValue(dataObject)
  })
于 2013-12-16T10:26:21.567 回答