将字符串json转换为Scala case类

时间:2019-03-09 14:28:30

标签: json scala

我知道这种问题已经回答了很多次,但是我找不到我得到的错误的答案::

我正在尝试在IntelliJ IDEA CE中将字符串JSON转换为case类。

代码像这样::

package com.netflix.ist.gbi.application

import scala.io.Source
import scala.collection.mutable._
import org.json4s._
//import org.json4s.DefaultFormats
import com.netflix.ist.gbi.model.EventPayloadIn
import org.json4s.jackson.JsonMethods._

object ObjDataArchival extends App{
    val a : ListBuffer[String] = ListBuffer()
    for (line Source.fromFile("/Users/sankar.biswas/Desktop/jsonFile.json").getLines) {
       a.append(line)
       jsonStrToMap(line)
    }


    def jsonStrToMap(jsonStr: String) : EventPayloadIn = {
       implicit val formats : DefaultFormats.type = org.json4s.DefaultFormats
       parse(jsonStr).extract[EventPayloadIn]
    }


}

案例类在已导入的scala文件中定义。 但是当我运行它时,出现以下错误::

Exception in thread "main" java.lang.NoSuchMethodError: scala.Function0.$init$(Lscala/Function0;)V
at org.json4s.ThreadLocal.<init>(Formats.scala:348)
at org.json4s.DefaultFormats.$init$(Formats.scala:355)
at org.json4s.DefaultFormats$.<init>(Formats.scala:333)
at org.json4s.DefaultFormats$.<clinit>(Formats.scala)
at com.apple.ist.gbi.application.ObjDataArchival$.jsonStrToMap(ObjDataArchival.scala:21)
at com.apple.ist.gbi.application.ObjDataArchival$$anonfun$1.apply(ObjDataArchival.scala:16)
at com.apple.ist.gbi.application.ObjDataArchival$$anonfun$1.apply(ObjDataArchival.scala:13)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at com.apple.ist.gbi.application.ObjDataArchival$.delayedEndpoint$com$apple$ist$gbi$application$ObjDataArchival$1(ObjDataArchival.scala:13)
at com.apple.ist.gbi.application.ObjDataArchival$delayedInit$body.apply(ObjDataArchival.scala:10)
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:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at com.apple.ist.gbi.application.ObjDataArchival$.main(ObjDataArchival.scala:10)
at com.apple.ist.gbi.application.ObjDataArchival.main(ObjDataArchival.scala)

错误似乎在这一行::

implicit val formats : DefaultFormats.type = org.json4s.DefaultFormats

我尝试使用类似::: p的行

implicit val formats = DefaultFormats

但是无法以任何方式解决该问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

如下所示的方法可以起作用。

import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.io.Source

object JSONParsing extends App {


  implicit val formats = DefaultFormats // Brings in default date formats etc.

  case class BookDetails(bookId: String, bookName: String, authorName: String, authorCountry: String)

  for (line <- Source.fromFile("/Users/sankar.biswas/Desktop/jsonFile.json").getLines) {
    val bookDetails = jsonStrToMap(line)
    println(bookDetails)
  }

  def jsonStrToMap(jsonStr: String): BookDetails = {
    parse(jsonStr).camelizeKeys.extract[BookDetails]
  }
}

jsonFile文件的内容:

{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}
{"book_id":"1","book_name":"Scala","author_name":"Edward","author_country":"Poland"}

更新:

build.sbt

  "org.json4s" %% "json4s-native" % "3.5.2",
  "org.json4s" %% "json4s-jackson" % "3.6.0"