尝试使用Lift-JSON从Scala Map生成JSON字符串时获取NoSuchMethodError。试图从scala cookbook执行以下程序
package com.sample
import net.liftweb.json.JsonAST
import net.liftweb.json.JsonDSL._
import net.liftweb.json.Printer.{compact,pretty}
object LiftJsonWithCollections extends App {
val json = List(1, 2, 3)
println(compact(JsonAST.render(json)))
val map = Map("fname" -> "Alvin", "lname" -> "Alexander")
println(compact(JsonAST.render(map)))
}
我得到以下错误
Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
at net.liftweb.json.Printer$class.layout$1(JsonAST.scala:597)
at net.liftweb.json.Printer$class.compact(JsonAST.scala:605)
at net.liftweb.json.Printer$.compact(JsonAST.scala:583)
at net.liftweb.json.Printer$class.compact(JsonAST.scala:590)
at net.liftweb.json.Printer$.compact(JsonAST.scala:583)
at com.sample.LiftJsonWithCollections$.delayedEndpoint$com$sample$LiftJsonWithCollections$1(LiftJsonWIthCollection.scala:10)
at com.sample.LiftJsonWithCollections$delayedInit$body.apply(LiftJsonWIthCollection.scala:8)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at com.sample.LiftJsonWithCollections$.main(LiftJsonWIthCollection.scala:8)
at com.sample.LiftJsonWithCollections.main(LiftJsonWIthCollection.scala)
无法解决它。
我在pom中添加了以下依赖项
<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-json_2.11</artifactId>
<version>3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json4s/json4s-jackson -->
<dependency>
<groupId>org.json4s</groupId>
<artifactId>json4s-jackson_2.11</artifactId>
<version>3.2.10</version>
</dependency>
当我删除JsonAST方法并编写以下代码时,它的工作原理..
object LiftJsonWithCollections extends App {
implicit val formats = DefaultFormats
val json = List(1, 2, 3)
**println(compact(render(json)))**
val map = Map("fname" -> "Alvin", "lname" -> "Alexander")
**println(compact(render(map)))**
}
但是我也可以在JsonAST API中找到一个render方法。它是如何工作的 ?