Reputation: 1122
I am using json-lift that is compatible with scala 2.10 from lift-json but I do not seem to have access to the extract method. like this example :
import net.liftweb.json._
object testobject {
case class process(process_id:Int,job_id:Int ,command:String, exception:String)
def main(args: Array[String]) {
val json = parse("""
{
"process_id": "2",
"job_id": "540",
"command": "update",
"exception": "0"
}
""")
json.extract[process] // produces an error
}
}
now the class has dynamic parsing , for example the following does not produce any error (sweet):
json.process_id // will produce JString(2)
my two questions are :
Update: the good people at lift have created an upgrade for scala 2.10.0 ... so you can just downloaded from their. No need for any work around.
import net.liftweb.json._
object testobject {
case class process(process_id:Int,job_id:Int ,command:String, exception:String)
def main(args: Array[String]) {
val json = parse("""
{
"process_id": "2",
"job_id": "540",
"command": "update",
"exception": "0"
}
""")
val p = json.extract[process] // maps the json object to the process case class
println(p.job_id) // will print 540
}
}
Upvotes: 1
Views: 176
Reputation: 238
I show you my method to get the proper String, hope helps you:
Suppose a list of tuples with x and y values
val dataSet:List[(Int,Int)] = new List((0,1),(1,3),(2,6))
I make my JObject (net.liftweb.json.JsonAST.JObject
):
val jsonTmp:JObject = ("x" -> dataSet.map(k => k._1)) ~ ("y" -> dataSet.map(k => k._2)))
then I get my String like this:
val jsonString:String = compact(render(jsonTmp))
compact(d:Document):String
& render(value:JValue):Document
are from json package.
And this is the resulting String (triple quotes are just for code formatting):
""" {"x":[0,1,2],"y":[1,3,6]} """
Upvotes: 1
Reputation: 1122
I got it:
val process_id = json.process_id match { case JString(s) => s.toInt }
Upvotes: 0