修改Spray JSON中的键

时间:2019-06-01 01:12:28

标签: scala akka-http spray-json

假设我有一个JSON对象,如下所示:

{ 
  "CATEGORY": "reference",
  "AUTHOR": "Nigel Rees",
  "TITLE": "Sayings of the Century",
  "PRICE": 8.95
}

这作为JsObject在系统中流动。使用Spray,我该如何小写这些字段?

1 个答案:

答案 0 :(得分:2)

此代码:

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"