Spark中字符串和json、map格式相互转化

Spark中字符串和json格式相互转化

字符串转为map

import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods.parse
val ad = """{"name":"zsd","age":11}"""
implicit val formats: DefaultFormats.type = org.json4s.DefaultFormats
val adMap: Map[String, Any] = parse(ad).extract[Map[String, Any]]

map或者case class 转化为字符串

import org.json4s.jackson.Serialization
import org.json4s.{Formats, NoTypeHints}
case class Person(name:String,age:Int)
val p = Person("zs",20)
implicit val formats: AnyRef with Formats = Serialization.formats(NoTypeHints)
val jsonString = Serialization.write(p)
val m = Map(
  "name" -> "zd",
  "age" -> 13
)
implicit val formats: AnyRef with Formats = Serialization.formats(NoTypeHints)
val jsonString = Serialization.write(m)

 

你可能感兴趣的:(Hadoop相关,spark,scala,spark,json,map)