简体   繁体   中英

Escaping JSON's Double-Quotes in Keys and Values?

Is there an equivalent JSON.stringify in Java/Scala?

I'd like to use this function as so:

JSON.stringify( JSON.stringify( { "a" : "have a nice day, \" sir!" } ) )
""{\"a\":\"have a nice day, \\\" sir!\"}""

Using spray-json :

scala> import spray.json._
import spray.json._

scala> def stringifyTwice(json: JsValue): String = 
     |   JsString(json.compactPrint).compactPrint
stringify: (json: spray.json.JsValue)String

scala> val obj: JsValue = JsObject("foo" -> JsString("bar"), "bippy" -> JsString("bap \" biz"))
obj: spray.json.JsValue = {"foo":"bar","bippy":"bap \" biz"}

scala> stringifyTwice(obj)
res0: String = "{\"foo\":\"bar\",\"bippy\":\"bap \\\" biz\"}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM