我不希望 JSON 在 C++ 中转义撇号。如何解决这个问题?
我在 JSON (libjson 6) 方面遇到了一些问题。在我制作的聊天信使中,服务器(由其他人制作)和桌面客户端之间存在冲突。尽管我总是使用引号来分隔字符串,但我客户端的 JSON 字符串始终会转义撇号。另一方面,服务器不希望撇号被转义,这会导致我的客户端发送 \'
的情况,这显然会给服务器的解析器带来问题。
我解决这个问题的唯一方法是让我的程序停止转义 JSON 消息中的撇号。然而,在谷歌和文档中搜索后,我没有找到任何东西。有人可以告诉我该怎么做吗?
I've encountered a bit of a problem with JSON (libjson 6). In a chat messenger I am making, there is a conflict between the server (which was made by other people) and the desktop client. My client's JSON strings always have apostrophes escaped, even though I always use quotes to delimit strings. The server, on the other hand, doesn't expect apostrophes to be escaped, which leads to situations where my client sends \'
which obviously creates problems for the server's parser.
The only way for me to solve this, is by making my program stop escaping apostrophes in JSON messages. However, after searching on Google and in the documentation, I haven't found anything. Can somebody tell me how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以删除撇号之前的转义符。
如果您在撇号之前从未转义过转义符(例如
\\'
表示“转义转义和未转义的撇号”),或者您的库总是转义它们,只需替换所有\'
与'
。有各种字符串替换函数,但这是一个简单的情况:否则,您将不得不解析字符串转义的子集,这更复杂,但并不难。
You can remove the escapes before the apostrophes.
If you never have escaped escapes before an apostrophe (e.g.
\\'
meaning "escaped-escape and unescaped apostrophe") or your library always escapes them, simply replace all\'
with'
. There are various string replace functions, but this is a simple case:Otherwise, you'll have to parse a subset of the string escapes, which is more involved, but not hard.