【问题标题】:Issue parsing JSON with Lift JSON使用 Lift JSON 解析 JSON 的问题
【发布时间】:2017-11-02 06:20:51
【问题描述】:

我正在尝试使用 Lift JSON 库解析 JSON。我通过将以下语句添加到我的 build.sbt 文件中,使用 SBT 导入了库:

libraryDependencies +="net.liftweb" % "lift-json" % "2.0"

我启动 SBT 并使用“控制台”命令运行 Scala 解释器。

然后我运行以下两条语句:

import net.liftweb.json._
parse(""" { "numbers" : [1, 2, 3, 4] } """)

在第二条语句之后,我收到以下错误:

<console>:11: error: not found: value parse
       parse(""" { "numbers" : [1, 2, 3, 4] } """)

为了确保我的项目没有问题,我开始了一个干净的项目,只导入了 Lift JSON 库。结果相同。我什至尝试了一个替代的 JSON 库(json4s),但是当它到达 parse 语句时它给出了完全相同的问题:-(

我正在运行以下版本: 斯卡拉 2.11.2 SBT 0.13.6 提升 JSON 2.0

有什么建议吗?

【问题讨论】:

    标签: scala lift-json


    【解决方案1】:

    Lift 2.0 已经很老了。只需使用 2.5 代替。 Afaict 2.0 实际上在 json 包对象中没有 parse 方法。

    【讨论】:

    • 由于您使用的是 Scala 2.11,因此您需要 Lift 3.0-M2。
    • 2.6也可以。
    【解决方案2】:

    来自带有 scala 版本 2.11.6 的 'scala repl' 和 Lift-json 的更高版本之一 (http://mvnrepository.com/artifact/net.liftweb/lift-json_2.11)

    从 lift-json README.md 文件运行示例 https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/

    您还可以按照 lift-json README.md 中的建议,使用“:require”(如下所示)将 paranamer.jar 文件添加到类路径

    $ scala
    
    Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> :require lift-json_2.11-3.0-M5-1.jar
    
    Added '<absolute path to>/lift-json/lift-json_2.11-3.0-M5-1.jar' to classpath.
    
    scala> import net.liftweb.json._
    
    import net.liftweb.json._
    
    scala> parse(""" { "numbers" : [1, 2, 3, 4] } """)
    
    res0: net.liftweb.json.JValue = JObject(List(JField(numbers,JArray(List(JInt(1), JInt(2), JInt(3), JInt(4)))))) 
    

    【讨论】: