当前位置:网站首页>scala之json数据解析
scala之json数据解析
2022-07-15 23:58:00 【欧阳小伙】
1、背景
使用scala写一些数据处理的时候,有的时候需要使用到json数据的解析。查了下资料,在spark或者flink中包含的有json解析工具包。如果只是scala的话,可能就需要添加上相应的json解析的包。
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-scala_2.13', version: '2.10.4'
> 官方文档: https://github.com/FasterXML/jackson-module-scala
## 2、案例
case class和json字符串互相转换。
```scala
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.{
DefaultScalaModule, ScalaObjectMapper}
case class Person(name: String, age :Int, habbit:List[String],isStudent: Boolean)
object JSONScalaDemo {
def main(args: Array[String]): Unit = {
val bigBoy = Person("shuaige", 18, List("Reading", "Swimming"), isStudent = true)
// object --> json
val jsonMapper = new ObjectMapper()
jsonMapper.registerModule(DefaultScalaModule)
val v = jsonMapper.writeValueAsString(bigBoy)
println(v)
// json--> object
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
val o = mapper.readValue[Map[String, Object]](v)
println(o)
}
}
## 3、总结
case class转为json的时候需要注意, 注册默认模型。DefaultScalaModule是一个Scala对象,支持所有当前Scala支持的数据类型。
```scala
val jsonMapper = new ObjectMapper()
jsonMapper.registerModule(DefaultScalaModule)
边栏推荐
- JS array object in goods_ With the same ID and an object, put goods_ size_ Add ID values together
- Flink (III) processing function
- 无线通信安全作业2
- 深圳开展建设工程合同网签试点,法大大助力建筑数字化
- 第1章:初识数据库与MySQL----MySQL安装
- Two bytes, carried out by the interviewer and shared with everyone
- [unity3d] toggle of ugui
- Reading the pointpillar code of openpcdet -- Part 2: network structure
- Valgrind
- Emqx server establishes ssl/tls secure connection, one-way and two-way
猜你喜欢
随机推荐
How does wechat applet realize pull-down refresh?
Flink(三)处理函数
【踩坑】Appending a row → DataFrame
Happy event, a long planned new era of shopping
伦敦银行情走势怎样产生
openpcdet之pointpillar代码阅读——第一篇:数据增强与数据处理
Redis入门
jol-core
Torch code template
Flink (VI) fault tolerance mechanism
The secret of the three moving averages in the spot gold trend chart
Leetcode day 22
英特尔助力开立医疗推动超声产检智能化
Envoy monitoring management
Talking about software defect management
UDS之0x22、0x2E服务
Flink (II) time and window
Uploading and downloading of files
02. Resttemplate learning notes
openpcdet之pointpillar代码阅读——第二篇:网络结构






![[JVM] garbage collector](/img/ed/06f34e734e70fa3a4f3b9229638947.png)


