使用lift-json将Json值提取为Map [英] Extract Json values as Map with lift-json

查看:153
本文介绍了使用lift-json将Json值提取为Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lift-json的文档建议我应该能够调用值"以将我当前的JObject结构获取为原始Scala映射.这种方法对我不起作用,因为值"的返回类型为json.Values而不是如示例所示的Map.我究竟做错了什么?是否需要隐式导入才能完成此转换?

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

推荐答案

由于某种原因,我在搜索中错过了与此重复项: 我可以吗使用Scala lift-json库将JSON解析为Map?

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?

答案必须明确地投射:

json.asInstanceOf[JObject].values

这篇关于使用lift-json将Json值提取为Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆