使用Jerkson时,字符串保持JSON的前导和尾随引号

时间:2022-09-13 19:27:02

JSON in question:

有问题的JSON:

{
"search_id": "",
"type": "Search.filter",
"query": "bar,club",
"params": {
    "search_id": "",
    "user_id": "",
    "client": "ios",
    "lat": 40.73199375351,
    "lon": -74.00080404533901,
    "radius": 20
}

}

Code to Retrieve the Data:

检索数据的代码:

val json = Json.parse(new String(body))
println((json \ "search_id") + " | " + (json \ "query"))
println(json)

printing just the json JsValue prints out the entire JSON as expected. printing out the first item produces: "" | "bar,club"

只打印json JsValue按预期打印出整个JSON。打印出第一个项目会产生:“”| “酒吧,俱乐部”

Why is it maintaining the quotes from JSON formatting? That's not part of the string, it's basically saying that the content inside the quotes is a string. How do I fix this?

为什么要保持JSON格式的引号?这不是字符串的一部分,它基本上是说引号内的内容是一个字符串。我该如何解决?

1 个解决方案

#1


8  

According to the doc, you should call .as[sometype] (unsafe conversion) or asOpt[sometype] (safe).

根据文档,您应该调用.as [sometype](不安全转换)或asOpt [sometype](安全)。

println((json \ "search_id").as[String] + " | " + (json \ "query").as[String])

#1


8  

According to the doc, you should call .as[sometype] (unsafe conversion) or asOpt[sometype] (safe).

根据文档,您应该调用.as [sometype](不安全转换)或asOpt [sometype](安全)。

println((json \ "search_id").as[String] + " | " + (json \ "query").as[String])