Having a jString : JString
value holding an "abc"
string inside I get "JString(abc)" : String
if I call jString.toString
. How do I get "abc" : String
instead?
How to convert a JSON JString value to an ordinary String in Lift?
16.8k Views Asked by Ivan At
4
There are 4 best solutions below
0

val jstring=JString("abc")
implicit val formats = net.liftweb.json.DefaultFormats
System.out.println(jstring.extract[String])
0

This was asked a while ago, but I wanted a simple one-line helper that would get my string for me in the context of an expression, so I wrote this little thing inside of an object called Get:
object Get {
def string(value: JValue): String = {
val JString(result) = value
result
}
...
}
This way I can just do, e.g., val myString = Get.string(jsonStringValue)
To extract a value from JValue you can use any method described here: What is the most straightforward way to parse JSON in Scala?
For instance:
You can use 'render' function to convert any JValue to printable format. Then either 'pretty' or 'compact' will convert that to a String.
or