Json字符串转换成Scala对象的一些注意事项

字符串:

{
    "currentTime":1604475400444,
    "dayString":"2020-11-04",
    "hmString":"16:31",
    "hmsString":"16:31:56",
    "itemAveragePrice":23.582698961937723,
    "itemCount":56,
    "itemSaleTotalNum":289,
    "itemSaleTotalPrice":6815.4000000000015,
    "liveId":286066171497,
    "shopCount":56
}

1、所需依赖:

<dependency>
            <groupId>org.json4s</groupId>
            <artifactId>json4s-jackson_2.10</artifactId>
            <version>3.2.10</version>
</dependency>

2、手动导包

import org.json4s._
import org.json4s.jackson.JsonMethods._

3、手动导入隐式值

 implicit val formats = DefaultFormats

4、转换成对象

val json = "要转换的json字符串"
val value = parse(json).extract[要转换的实体类型] //要提前定义好接收数据的样例类
 //可以对转换后的对象的属性进行操作
value.setSaleNum(value.getSaleNum+1L)
value.setSalesVolume(value.getSalesVolume+2D)
原文地址:https://www.cnblogs.com/atBruce/p/13926719.html