在Eclipse Scala IDE中使用工作表(包含代码和输出)。
这个简单的代码给出ClassNotFoundException
。出于某种原因,它似乎无法找到Address类。
类似:Deserializing JSON into user-defined case classes with Jerkson(但他们的问题与Play框架和解析错误有关)
import com.codahale.jerkson.Json._
object person {
case class Address(city: String, state: String)
case class Person(name: String, age: Int, addresses: Seq[Address])
parse[Person]("""{"name":"James", "age":25, "addresses": [{"city":"New York", "state": "NY"}, {"city":"Brooklyn", "state": "NY"}]}""")
//> java.lang.ClassNotFoundException: <empty>.person.Address
//| at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
//| at java.security.AccessController.doPrivileged(Native Method)
//| at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
//| at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
//| at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
//| at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
//| at com.codahale.jerkson.util.CaseClassSigParser$.loadClass(CaseClassSigP
//| arser.scala:186)
//| at com.codahale.jerkson.util.CaseClassSigParser$.typeRef2JavaType(CaseCl
//| assSigParser.scala:143)
//| at com.codahale.jerkson.util.CaseClassSigParser$$anonfun$typeRef2JavaTyp
//| e$1.apply(CaseClassSigParser.scala:146)
//| at com.codahale.jerkson.util.CaseClassSigParser$$anonfun$typeRef2JavaTyp
//| e$1.apply(CaseClassSigParser.scala:146)
//| at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
//| .scala:233)
//|
//| Output exceeds cutoff limit.
}
答案 0 :(得分:0)
我通过使用lift-json jar使其工作。请参阅lift-json文档中的extracting values。
package com.example.json
import net.liftweb.json._
object HelloJson extends App {
case class Address(city: String, state: String)
case class Person(name: String, addresses: List[Address])
implicit val formats = DefaultFormats
val json = parse(""" { "name" : "Billy", "addresses" : [{"city": "New York", "state": "NY"}, {"city": "Washington", "state": "DC"}] } """)
println(json.extract[Person])
}