Scala 在JSON中修改键
假设我有一个JSON对象,如下所示:Scala 在JSON中修改键,scala,akka-http,spray-json,Scala,Akka Http,Spray Json,假设我有一个JSON对象,如下所示: { "CATEGORY": "reference", "AUTHOR": "Nigel Rees", "TITLE": "Sayings of the Century", "PRICE": 8.95 } 这在系统中作为JsObject流动。使用Spray,如何将这些字段小写?此代码: import spray.json._ val str = """{"CATEGORY": "reference", "AUTHO
{
"CATEGORY": "reference",
"AUTHOR": "Nigel Rees",
"TITLE": "Sayings of the Century",
"PRICE": 8.95
}
这在系统中作为JsObject
流动。使用Spray,如何将这些字段小写?此代码:
import spray.json._
val str =
"""{"CATEGORY": "reference",
"AUTHOR": "Nigel Rees",
"TITLE": "Sayings of the Century",
"PRICE": 8.95}"""
def main(args: Array[String]) : Unit =
println(JsObject(str.parseJson.asJsObject.fields.map(el => el._1.asInstanceOf[String].toLowerCase -> el._2 )))
给出:
{"author":"Nigel Rees","category":"reference","price":8.95,"title":"Sayings of the Century"}
在sbt中具有以下依赖性:
"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"
此代码:
import spray.json._
val str =
"""{"CATEGORY": "reference",
"AUTHOR": "Nigel Rees",
"TITLE": "Sayings of the Century",
"PRICE": 8.95}"""
def main(args: Array[String]) : Unit =
println(JsObject(str.parseJson.asJsObject.fields.map(el => el._1.asInstanceOf[String].toLowerCase -> el._2 )))
给出:
{"author":"Nigel Rees","category":"reference","price":8.95,"title":"Sayings of the Century"}
在sbt中具有以下依赖性:
"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"