2010-08-26 35 views
3

我们正在构建一个iPhone聊天应用程序。为什么Objective-C将JSON值转换为ASCII字符代码的散列?

当从浏览器发送到iPhone一个JSON聊天消息:

{"content":"Hi"} 

iPhone的接收:

{"content":{"0":72,"1":105,"length":2}} 

但是,我们打算为它收到相同的确切消息。

要重现此问题,请先安装node.js & redis。然后:

  • 获取代码:

    git clone git://github.com/acani/acani.git 
    cd acani 
    git submodule update --init 
    
  • 开始Redis的默认端口上。

  • 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. 
    
  • 位于http://github.com/acani/acani-chat/tree/master/Lovers2/打开Lovers.xcodeproj,并改变LoversAppDelegate.m最初加载ChatViewController代替HomeViewController的。

    homeViewController = [[HomeViewController alloc] init]; # comment out this line 
    # change the next line to: 
    navigationController = [[UINavigationController alloc] initWithRootViewController:[[ChatViewController alloc] init]]; 
    # Then, build & run. 
    
+0

您可以在实际解析JSON的地方显示相关的Objective-C代码吗?您使用的是哪种JSON框架? – 2010-08-26 02:56:10

+0

这不是JSON字符串的转换到NSDictionary中;这样可行。我知道,因为我在这行之后放了一个NSLog:http://github.com/acani/acani-chat/blob/master/Lovers2/ZTWebSocket.m#L207它显示JSON字符串的值的字符有被翻译成ASCII码。 – ma11hew28 2010-08-27 14:20:39

回答

6

我们知道了,它不是iPhone或Objective- C中,转换错误发生在node.js服务器上,我们忘记在JSON对象的字符串值周围加引号,因此JSON.stringify() JavaScript函数正在转换字符串,如上所示...除了我们正在做的如:{"content":Hi}。当我们将其更改为:{"content":"Hi"}时,它工作正常。Duhh ...

1

我的猜测是,你需要转义字符串(JSON)被发送,在收到使用stringByAddingPercentEscapesUsingEncoding然后UNESCAPE它。

前三个数字是072 - 十进制数是'H'。这让我觉得“由于没有编码的传输可能会丢失”,还有其他的东西反对这个理论,但我认为这是值得一看的

+0

这很有道理。谢谢。我今晚会尝试并报告。我想我认为AsyncSocket库会照顾到这一点...... – ma11hew28 2010-08-27 14:21:16