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.
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…