Extract Json values as Map with lift-json

628 views
Skip to first unread message

Gregg Carrier

unread,
Apr 6, 2011, 5:35:04 PM4/6/11
to lif...@googlegroups.com
The documentation for lift-json suggests that I should be able to call 
'values' to get my current JObject structure as a vanilla Scala Map.
This approach is not working for me, as the return type of 'values' is
json.Values rather than a Map as the examples show. What am I doing
wrong?


scala
> val json = parse("""{"k1":"v1","k2":"v2"}""")        
json
: net.liftweb.json.package.JValue = JObject(List(JField(k1,JString(v1)), JField(k2,JString(v2))))

scala
> json.values                                  
res4
: json.Values = Map((k1,v1), (k2,v2))

scala
> res4.get("k1")                                        
<console>:18: error: value get is not a member of json.Values
   res4
.get("k1")

David Pollak

unread,
Apr 6, 2011, 5:50:00 PM4/6/11
to lif...@googlegroups.com

Looks like a REPL problem.  Try compiling the same code.
 

   res4
.get("k1")

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



--
Lift, the simply functional web framework http://liftweb.net

Joni Freeman

unread,
Apr 11, 2011, 11:17:02 AM4/11/11
to lif...@googlegroups.com
Hi,

'parse' function returns JValue, not JObject. You can cast it to JObject
if you are sure that the JSON you are parsing is a JSON object. The
static type of 'values' function is then known to be a Map.

scala> val json = parse("""{"k1":"v1","k2":"v2"}""")
json: net.liftweb.json.package.JValue =
JObject(List(JField(k1,JString(v1)), JField(k2,JString(v2))))

scala> json.asInstanceOf[JObject].values
res2: scala.collection.immutable.Map[String,Any] = Map((k1,v1), (k2,v2))

Cheers Joni

> --
> You received this message because you are subscribed to the Google
> Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb

> +unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages