iOS JSON在实际设备上遇到麻烦 [英] iOS JSON troubles on actual device

查看:297
本文介绍了iOS JSON在实际设备上遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,这适用于运行iOS 6+的模拟器,但不适用于运行iOS 5+的实际iPad。当我在iPad上运行它的应用程序时,它会给我这个错误(和其他人)。

 解析JSON时出错:Error Domain = NSCocoaErrorDomain Code = 3840无法完成该操作。(Cocoa错误3840.)(围绕字符316的错误对象)UserInfo = 0x650260 {NSDebugDescription =字符316周围形成错误的对象} 

使用的JSON是这个(从Javascript传递):{functionname:setItem,args:{key:http:// localhost:3000 / assets / tasks / task.js value:function Task(){this.steps = new Array,this.completed =!1,this.loaded =!1,this.stepCount = -1,this.points = 0,this.newpoints = 0,this .prevpoints = 0,this.circleGesture =!1,this.customGesture =!1,this.subtaskCounter = 0,this.className = //}(继续一段时间,因为它是一个js脚本)}}

在iOS中将json字符串转换成json的代码

  if([[urlStr lowercaseString] hasPrefix:protocolPrefix])
{
//从URL中剥离协议。我们将获得调用本地方法的输入
urlStr = [urlStr substringFromIndex:protocolPrefix.length];

//解码url字符串
urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSError * jsonError;

//在URL中解析JSON输入
NSDictionary * callInfo = [NSJSONSerialization
JSONObjectWithData:[urlStr dataUsingEncoding:NSUTF8StringEncoding]
选项:0
错误:& jsonError]

//检查解析JSON输入是否有错误
if(jsonError!= nil)
{
NSLog(@解析JSON时出错:%@ ,[jsonError debugDescription]);
NSLog(@%@,urlStr);
return NO;
}

....
}

CoffeeScript

  callNativeFunction:(func,args,callback) - > 
if global_phone_os =='ios'
url =js2native://
callInfo = {}
callInfo.functionname = func
如果args?
callInfo.args = args
如果回调?
cid = Math.random()。toString(36).substr(2,10)
@callback [cid] = callback
callInfo.callback_id = cid
url + JSON.stringify callInfo
@openCustomURLinIFrame url

任何想法如何让这两个仿真器和实际设备?



UPDATE



。如果我这样做,这将被标记为答案。我必须在coffeescript中执行以下操作:

  url + = encodeURIComponent JSON.stringify callInfo 



这有所作为。



谢谢。

解决方案

这解决了问题。

  url + = encodeURIComponent JSON.stringify callInfo 


For some reason, this works on the emulator running iOS 6+ but not on an actual iPad running iOS 5+. When I run it my app on the iPad it gives me this error (and others).

Error parsing JSON: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 316.) UserInfo=0x650260 {NSDebugDescription=Badly formed object around character 316.}

JSON used is this (which is passed from Javascript): {"functionname":"setItem","args":{"key":"http://localhost:3000/assets/tasks/task.js","value":"function Task(){this.steps=new Array,this.completed=!1,this.loaded=!1,this.stepCount=-1,this.points=0,this.newpoints=0,this.prevpoints=0,this.circleGesture=!1,this.customGesture=!1,this.subtaskCounter=0,this.className=/"/"}(goes on for a while since it is a js script)"}}

Code to convert json string to json in iOS

if ([[urlStr lowercaseString] hasPrefix:protocolPrefix])
{
    //strip protocol from the URL. We will get input to call a native method
    urlStr = [urlStr substringFromIndex:protocolPrefix.length];

    //Decode the url string
    urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSError *jsonError;

    //parse JSON input in the URL
    NSDictionary *callInfo = [NSJSONSerialization
                              JSONObjectWithData:[urlStr dataUsingEncoding:NSUTF8StringEncoding]
                              options:0
                              error:&jsonError];

    //check if there was error in parsing JSON input
    if (jsonError != nil)
    {
        NSLog(@"Error parsing JSON: %@",[jsonError debugDescription]);
        NSLog(@"%@", urlStr);
        return NO;
    } 

    ....
}

CoffeeScript

callNativeFunction: (func, args, callback)->
  if global_phone_os == 'ios'
    url = "js2native://"
    callInfo = {}
    callInfo.functionname = func
    if args?
      callInfo.args = args
    if callback?
      cid = Math.random().toString(36).substr(2,10)
      @callback[cid] = callback
      callInfo.callback_id = cid
    url += JSON.stringify callInfo
    @openCustomURLinIFrame url

Any ideas how to get this to work on both emulator and actual device?

UPDATE

So far I think I fixed it. If I did, this will be marked as the answer. I had to do the following in the coffeescript:

url += encodeURIComponent JSON.stringify callInfo

That made a difference.

Thanks.

解决方案

This did fix the issue.

url += encodeURIComponent JSON.stringify callInfo

这篇关于iOS JSON在实际设备上遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆