import org.json4s.native.Serialization.{read, write} object Json2CaseClass extends App { implicit val formats = org.json4s.DefaultFormats case class Class(_name:String, students: List[Student]) case class Student(sid:String, _name:String) val s = "{/"_name/":/"Class1/",/"students/":[{/"sid/":/"1/",/"_name/":/"小明/"},{/"sid/":/"1/",/"_name/":/"小王/"}]}" val clazz:Class = read[Class](s) println(clazz) println(write(clazz)) }
<dependency> <groupId>org.json4s</groupId> <artifactId>json4s-native_2.10</artifactId> <version>3.2.11</version> <scope>${pkg}</scope> </dependency>
代码工程:https://github.com/chocolateBlack/LearningSpark/commit/3c56dd2c89c83b99fb9b3de6d563065279ca2a0e
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/tech/opensource/185743.html