I am experimenting with the json4s library (based on lift-jsn). I'm one of those things to parse a JSON string in AST, and then manipulate it. For example, I want to upset a field (put the field in AST if it does not exist, or update its value if it does so).
I have not figured out how to do this in the document. Experiment with available methods, I came up with the following, which works but seems unnecessary.
import org.json4s._ import org.json4s.JsonDSL._ import org.json4s Basic. JasonMedish._Object TestJason {Intimate Val Format = Default Format DIF Main (Args: Array [String]): Unit = {Wall Jason = "" {"FU": 1, "Bar": {"Foo": 2}} " "" Atl et pars (jsn) .asInstanceOf [Jobbject] println (upsertField (ast, ("foo" -> "3")) println (upsertField (ast, ("foobar" -> "3 ") () ())}} Def upsertField (src: Jobbject, fld: JField): jvvu = {if (src \ fld._1 == JNothing) {src ~ fld} else {src.replace (list (fld._1), many For reasons I dislike: -
To explicitly insert the results of pars (JSN) / cod E> to jabax - The result of the
upsertfield function is a JValue for which I must reload the object I want to manipulate - The
upsertfield function just feels
Merge function that creates or overrides a field, you can also update the fields that are not at the root level of the tree.
import org.json4s._ import org.json4s.JsonDSL._ import org.json4s.jackson.JsonMethods._Object mergeson app {val Json = "" "| {|" Foo ": 1, |" Bar ": {|" Foo ": 2 |} |} |" Expands "" ".stripMargin val ast = parse (json) val update = ast merge ((" foo ", 3) ~ (" bar ", (" fnord ", 5)) println (beautiful) / {// "Foo": 3, // "bar": {// "foo": 2, // "fnord": 5 //}}} / Html>
No comments:
Post a Comment