热门IT资讯网

【总结】使用Scala实现Json与Case Class相互转换

发表于:2024-11-22 作者:热门IT资讯网编辑
编辑最后更新 2024年11月22日,import org.json4s.native.Serialization.{read, write}object Json2CaseClass extends App { implicit va
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))}
    org.json4s    json4s-native_2.10    3.2.11    ${pkg}

代码工程:https://github.com/chocolateBlack/LearningSpark/commit/3c56dd2c89c83b99fb9b3de6d563065279ca2a0e


0