Extract Json values as Map with lift-json

2020-03-21 12:57发布

问题:

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? Is there an implicit import necessary to accomplish this conversion?

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")

回答1:

Somehow I missed the duplicate of this in my search: Can I use the Scala lift-json library to parse a JSON into a Map?

Answer is to cast explicitly:

json.asInstanceOf[JObject].values