首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >case class 类中有Map怎么转json?

case class 类中有Map怎么转json?

提问于 2022-08-24 21:26:47
回答 1关注 0查看 177
代码语言:java
复制
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,还有哪种更优雅的方式,谢谢。

相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档