为什么 Objective-C 将 JSON 值转换为 ASCII 字符代码的哈希值?
我们正在构建一个 iPhone 聊天应用程序。
当从浏览器向 iPhone 发送 JSON 聊天消息时:
{"content":"Hi"}
iPhone 接收:
{"content":{"0":72,"1":105,"length":2}}
但是,我们希望它接收完全相同的消息。
要重现此问题,请首先安装node.js &雷迪斯。然后:
获取代码:
git 克隆 git://github.com/acani/acani.git cd阿卡尼 git 子模块更新 --init
在默认端口上启动Redis。
来自http://github.com/acani/acani-node:
node acani-node-server.js # 运行node.js聊天服务器 # 在 Google Chrome 或 Firefox 中打开 index.html 并按照说明进行操作。
打开位于 http://github 的 Lovers.xcodeproj .com/acani/acani-chat/tree/master/Lovers2/,并更改 LoversAppDelegate.m 以最初加载 ChatViewController 而不是 HomeViewController。
homeViewController = [[HomeViewController alloc] init]; # 注释掉这一行 # 将下一行更改为: navigationController = [[UINavigationController alloc] initWithRootViewController:[[ChatViewController alloc] init]]; # 然后,构建并构建跑步。
We are building an iPhone chat application.
When sending from the browser to the iPhone a JSON chat message:
{"content":"Hi"}
The iPhone receives:
{"content":{"0":72,"1":105,"length":2}}
But, we intend for it to receive the same exact message.
To reproduce this issue, first install node.js & redis. Then:
Get the code:
git clone git://github.com/acani/acani.git cd acani git submodule update --init
Start Redis on the default port.
From http://github.com/acani/acani-node:
node acani-node-server.js # run node.js chat server # open index.html in a Google Chrome or Firefox and follow instructions.
Open Lovers.xcodeproj located in http://github.com/acani/acani-chat/tree/master/Lovers2/, and change LoversAppDelegate.m to initially load the ChatViewController instead of the HomeViewController.
homeViewController = [[HomeViewController alloc] init]; # comment out this line # change the next line to: navigationController = [[UINavigationController alloc] initWithRootViewController:[[ChatViewController alloc] init]]; # Then, build & run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们想通了。它根本不是 iPhone 或 Objective-C。 Node.js 服务器上发生转换错误。我们忘记在 JSON 对象的字符串值两边加引号,因此
JSON.stringify()
JavaScript 函数正在转换字符串,如上所示...除了我们正在执行以下操作: >{“内容”:嗨}。当我们将其更改为:{"content":"Hi"}
时,效果很好。呃……We figured it out. It wasn't the iPhone or Objective-C at all. The conversion error was happening on the node.js server. We forgot to put quotes around the string values of the JSON object, and so the
JSON.stringify()
JavaScript function was converting the strings as shown above... except we were doing something like:{"content":Hi}
. When we changed it to:{"content":"Hi"}
, it worked fine. Duhh...我的猜测是您需要使用 stringByAddingPercentEscapesUsingEncoding 对发送的字符串 (JSON) 进行转义,然后在收到时对其进行转义。
前三个数字是 072 - 十进制表示“H”。这让我认为“可能会由于没有编码的传输而丢失。还有其他一些反对这一理论的事情,但我认为值得一看。
My guess is you need to escape the string (JSON) being sent, using stringByAddingPercentEscapesUsingEncoding and then unescape it on receipt.
The first three numbers are 072 - in decimal that's 'H'. Which makes me think a " might be getting lost due to transmission without encoding. There are other things against this theory but I think it is worth looking at.