使用 lift-json 将 Json 值提取为 Map
lift-json 的文档表明我应该能够调用“values”来获取当前的 JObject 结构作为普通的 Scala Map。这种方法对我来说不起作用,因为“values”的返回类型是 json.Values 而不是示例所示的 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.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")
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知怎的,我在搜索中错过了这个重复项:
我可以吗使用 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?
Answer is to cast explicitly: