json4s org.json4s.package $ MappingException:不知道如何将JString转换为double

时间:2017-01-12 14:10:48

标签: json scala jackson json4s jackson2

我正在尝试一个非常简单的例子,但不能使它工作;下面是我的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

1 个答案:

答案 0 :(得分: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())

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