代码之家  ›  专栏  ›  技术社区  ›  ma11hew28

为什么Objective-C要将JSON值转换成ASCII字符码的散列?

  •  3
  • ma11hew28  · 技术社区  · 15 年前

    从浏览器向iPhone发送JSON聊天信息时:

        {"content":"Hi"}
    

    iPhone接收:

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

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

    • 获取代码:

      git clone git://github.com/acani/acani.git
      cd acani
      git submodule update --init
      
    • 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/ ,并将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.
      
    2 回复  |  直到 14 年前
        1
  •  6
  •   ma11hew28    15 年前

    我们想出来了。根本不是iPhone或Objective-C。转换错误发生在节点.js服务器。我们忘了在JSON对象的字符串值周围加引号,所以 JSON.stringify() JavaScript函数正在转换字符串,如上图所示。。。但我们做的是: {"content":Hi} . 当我们把它改成: {"content":"Hi"} ,效果不错。嗯。。。

        2
  •  1
  •   Adam Eberbach Adil Shaikh    15 年前

    我猜您需要使用stringByAddingPercentEscapesUsingEncoding转义发送的字符串(JSON),然后在收到时取消转义。

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