我正在使用json-spray.似乎当我尝试打印解析的JsString值时,它包含字符串上的书籍结尾引号.
val x1 = """ {"key1": "value1","key2": 4} """ println(x1.asJson) println(x1.asJson.convertTo[Map[String,JsValue]])
哪个输出:
{"key1":"value1","key2":4} Map(key1 -> "value1",key2 -> 4)
但这意味着key1的字符串值实际上是引用的,因为scala显示没有引号的字符串.即val k =“value1”输出:value1而不是“value1”.也许我做错了什么,但我能想出的最好的解决方法如下:
val m = x1.asJson.convertTo[Map[String,JsValue]] val z = m.map({ case(x,y) => { val ny = y.toString( x => x match { case v: JsString => v.toString().tail.init case v => v.toString() } ) (x,ny) }}) println(z)
Map(key1 -> value1,key2 -> 4)
解决方法
试试这个:
import spray.json._ import DefaultJsonProtocol._ val jsString = new JsString("hello") val string = jsString.convertTo[String]