JSONObject getString返回错误的字符串[duplicate]

时间:2021-08-03 22:44:21

This question already has an answer here:

这个问题在这里已有答案:

In one of my Android projects, I hit a web service and as a response I get a simple key value pair like

在我的一个Android项目中,我点击了一个Web服务,作为响应,我获得了一个简单的键值对

{"CHECKSUM":"8d40oGKDPqs8sBAd6ky9tF40h\/111XaDKpqR\/Bzme17="}

but when I try to read it via JSONObject.getString("CHECKSUM") method it omits backward slash characters and returns the remaining string.

但是当我尝试通过JSONObject.getString(“CHECKSUM”)方法读取它时,它会省略反斜杠字符并返回剩余的字符串。

Is this a bug in JSONObject implementation?

这是JSONObject实现中的错误吗?

1 个解决方案

#1


This is the correct implementation of JSONObject.getString. The backslashes are being read as an escape character for the forward slashes. If for some reason it is imperative that you get the backlashes as part of your response, you would need your backend web developers to change the response from "\/" to "\\\/"

这是JSONObject.getString的正确实现。反斜杠被读作正斜杠的转义字符。如果出于某种原因必须将反斜杠作为响应的一部分,则需要后端Web开发人员将响应从“\ /”更改为“\\\ /”

#1


This is the correct implementation of JSONObject.getString. The backslashes are being read as an escape character for the forward slashes. If for some reason it is imperative that you get the backlashes as part of your response, you would need your backend web developers to change the response from "\/" to "\\\/"

这是JSONObject.getString的正确实现。反斜杠被读作正斜杠的转义字符。如果出于某种原因必须将反斜杠作为响应的一部分,则需要后端Web开发人员将响应从“\ /”更改为“\\\ /”