import scala.beans.BeanProperty
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import scala.collection.mutable
case class EventLog(
@BeanProperty var guid: Long,
@BeanProperty var sessionId: String,
@BeanProperty var eventId: String,
@BeanProperty var timeStamp: Long,
@BeanProperty var eventInfo: Map[String, String] )
val events: List[String] = List("appLaunch","pageLoad","adShow","adClick","itemShare","itemCollect","putBack","wakeUp","appClose")
val eventInfoMap: mutable.HashMap[String, String] = new mutable.HashMap()
val eventLog = EventLog(
RandomUtils.nextLong(1, 1000),
RandomStringUtils.randomAlphabetic(12).toUpperCase(),
events(RandomUtils.nextInt(0, events.length)),
System.currentTimeMillis(),
eventInfoMap.toMap
)
val j = JSON.toJSONString(eventLog, SerializerFeature.QuoteFieldNames)
// {"eventId":"wakeUp","eventInfo":{"empty":false,"traversableAgain":true},"guid":100,"sessionId":"NGTPNWELLDYK","timeStamp":1661333145132}
遇到的情况是 case class 类 eventLog 在转json时,其中的map eventInfo转换异常。
劳驾大佬解答,另外请教 scala case class 转 json,还有哪种更优雅的方式,谢谢。
相似问题