[英]Parse json string into scala case class in java
I have a small spring(mvc) web application. 我有一个小的spring(mvc)Web应用程序。 This application has dependency on one scala module, which has a
case class
MessageCommand. 此应用程序依赖于一个scala模块,该模块具有
case class
MessageCommand。 I want to convert the JSON String into MessageCommand Case class using Jackson
. 我想使用
Jackson
将JSON String转换为MessageCommand Case类。
Is this possible? 这可能吗?
I am using Spring 4, Jackson-asl 1.9, Scala 2.11 Case class. 我正在使用Spring 4,Jackson-asl 1.9,Scala 2.11 Case类。
Well you can. 好吧,你可以。 But you need to tweak some things in case class:
但是您需要对case类进行一些调整:
case class Message(@BeanProperty var num:Int, @BeanProperty var name:String){
def this() = this(-1,null)
}
import org.codehaus.jackson.map.ObjectMapper
import scala.beans.BeanProperty
val mapper = new ObjectMapper
val json = """{"num":1,"name":"Di Caprio"}"""
scala> val user = mapper.readValue(json, classOf[Message])
user: Message = Message(1,Di Caprio)
If you cannot tweak the case class, then it gets a bit difficult and untidy 如果您无法调整案例类,那么它将变得有点困难和不整洁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.