如何使用scala将其发送到JSON解析器并使其正确解析?似乎没有正确处理反斜杠。
val s: String = "{\"fields\":[{\"acct-num\":\"28687008\"},{\"cust-id\":\"8\"},{\"quantity\":\"1\"},{\"ord-count\":\"7\"}]}"
val objectMapper = new ObjectMapper()
val ba1: Array[Byte] = objectMapper.writeValueAsBytes(s)
val elem = objectMapper.readValue(ba1, classOf[JsonNode]).get("fields").findValue("acct-num").asText()
返回NPE
java.lang.NullPointerException
at test.A$A3$A$A3.elem$lzycompute(scalatest.sc:91)
at test.A$A3$A$A3.elem(scalatest.sc:91)
at test.A$A3$A$A3.get$$instance$$elem(scalatest.sc:91)
at #worksheet#.#worksheet#(scalatest.sc:244)
那是因为出于某种原因,JSON值未按原样处理转义字符。
val node = objectMapper.readValue(ba1, classOf[JsonNode]).toString
返回
node: String = "{\"fields\":[{\"acct-num\":\"28687008\"},{\"cust-id\":\"8\"},{\"quantity\":\"1\"},{\"ord-count\":\"7\"}]}"