如何根据兄弟属性使用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)
答案 0 :(得分:2)
以下是可能的解决方案:
1)首先找到类型为html的数据json
2)转换json值子
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)
})