2013-12-15 25 views
0

如何使用基於同級屬性的scala lift來轉換下面的json? 在下面的JSON,我想價值在「值」編碼屬性如果同級屬性「類別」爲「HTML」如何使用scala lift來轉換json?

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) 

回答

2

下面是可能的解決方案:

1)先找到與HTML類型

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) 
    }) 
+0

JSON數據的偉大工程!謝謝! –

相關問題