【问题标题】:NoSuchMethodError while trying to generate JSON strings from a Scala Map using Lift-JSON尝试使用 Lift-JSON 从 Scala Map 生成 JSON 字符串时出现 NoSuchMethodError
【发布时间】:2019-02-07 10:24:43
【问题描述】:

尝试使用 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>

【问题讨论】:

    标签: json scala collections lift-json


    【解决方案1】:

    当我删除 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 中找到渲染方法。它是如何工作的?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多