3

我正在尝试一个非常简单的示例,但无法使其工作;下面是我的 JSON 格式,我使用 Scala json4s 解析器来解析 JSON;如果任何 JSON 字段不是字符串,则 json4s 无法解析 JSON 字符串。例如 json4s 无法将 field_3 解析为 double 并将 field_4 解析为 int。

val jsonStr="""{ "data" : [
  {
    "field_1"  : "value_1",
    "field_2" : "value_2",
    "field_3"     : "76.7",
    "field_4"   : "8320"
  }
 ,{
    "field_1"  : "value_1",
    "field_2" : "value_2",
    "field_3"     : "96.5",
    "field_4"   : "128"
  }
 ,{
    "field_1"  : "value_1",
    "field_2" : "value_2",
    "field_3"     : "84.5",
    "field_4"   : "8320"
  }
  ] }"""

   case class TestClass(field_1: String, field_2: String, field_3: Double, field_4: Int) {
    override def toString = s"{field_1:$field_1,field_2:$field_2,field_3:$field_3,field_4:$field_4}"
  }

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
  implicit val formats = DefaultFormats
  val JSON = parse(jsonStr)\"data"
  val info=JSON.extract[List[TestClass]]
  println(info)


  Exception in thread "main" org.json4s.package$MappingException: No usable value for value
Do not know how to convert JString(76.7) into double
    at org.json4s.reflect.package$.fail(package.scala:96)
    at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:443)
    at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:463)
    at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:463)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
    at scala.collection.AbstractTraversable.map(Traversable.scala:105)
    at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$instantiate(Extraction.scala:451)
    at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:491)
    at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:488)
    at org.json4s.Extraction$.org$json4s$Extraction$$customOrElse(Extraction.scala:500)
    at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:488)
    at org.json4s.Extraction$.extract(Extraction.scala:332)
    at org.json4s.Extraction$CollectionBuilder$$anonfun$6.apply(Extraction.scala:341)
    at org.json4s.Extraction$CollectionBuilder$$anonfun$6.apply(Extraction.scala:341)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
    at scala.collection.AbstractTraversable.map(Traversable.scala:105)
    at org.json4s.Extraction$CollectionBuilder.mkCollection(Extraction.scala:341)
    at org.json4s.Extraction$CollectionBuilder.result(Extraction.scala:361)
    at org.json4s.Extraction$.extract(Extraction.scala:320)
    at org.json4s.Extraction$.extract(Extraction.scala:42)
    at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
    at com.spark.demo.JsonTest$.json4Parser(JsonTest.scala:149)
    at com.spark.demo.JsonTest$.main(JsonTest.scala:136)
    at com.spark.demo.JsonTest.main(JsonTest.scala)
Caused by: org.json4s.package$MappingException: Do not know how to convert JString(76.7) into double
    at org.json4s.Extraction$.convert(Extraction.scala:559)
    at org.json4s.Extraction$.extract(Extraction.scala:331)
    at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:431)
    ... 29 more
4

1 回答 1

1

我已经使用类似的方法解决了这个问题:

case class TestClassHelper(field_1: String, field_2: String, field_3: String, field_4: String) {
    def toTestClass: TestClass(field_1, field_2, field_3.toDouble, field_4.toInt)

}
val infoTemp: List[TestClassHelper] =JSON.extract[List[TestClassHelper]]
val info: List[TestClass] = infoTemp.map(_.toTestClass())

也许有一个更优雅的解决方案,但这可以完成工作。

于 2017-01-23T22:51:49.883 回答