Extract Json values as Map with lift-json

左心房为你撑大大i 提交于 2020-01-11 05:14:09

问题


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


来源:https://stackoverflow.com/questions/5571259/extract-json-values-as-map-with-lift-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!