Scala: convert map to case class

前端 未结 6 2088
说谎
说谎 2020-12-01 01:53

Let\'s say I have this example case class

case class Test(key1: Int, key2: String, key3: String)

And I have a map

myMap = M         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 02:22

    This works well for me,if you use jackson for scala:

    def from[T](map: Map[String, Any])(implicit m: Manifest[T]): T = {
      val mapper = new ObjectMapper() with ScalaObjectMapper
      mapper.convertValue(map)
    }
    

    Reference from:Convert a Map to a POJO

提交回复
热议问题