【问题标题】:iOS JSON troubles on actual device实际设备上的 iOS JSON 问题
【发布时间】:2013-08-19 10:33:11
【问题描述】:

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

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)"}}

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

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;
    } 

    ....
}

咖啡脚本

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

任何想法如何让它在模拟器和实际设备上工作?

更新

到目前为止,我想我已经解决了。如果我这样做了,这将被标记为答案。我必须在咖啡脚本中执行以下操作:

url += encodeURIComponent JSON.stringify callInfo

这很重要。

谢谢。

【问题讨论】:

  • 老兄,听说你喜欢 JSON。 为什么你的 JSON 字符串中有 JSON 字符串?您的字符串的第 42 个字符是第一个转义的 " 在嵌入的 JSON 字符串中,因此问题显然与您对该数据的转义有关。
  • @AlexWayne 那么是coffeeScript 还是objective-c 代码中的问题?
  • 看起来像objective-c 代码,因为我刚刚在coffeescript.org 中进行了测试,我可以在json 中执行JSON.stringify 并将其字符串化。然后我可以正确地恢复所有这些。
  • 技术问题出在你的 ObjC 中的某个地方。但我认为有一个逻辑问题是你的咖啡脚本。在这种情况下,您的 JSON 中不应该需要 JSON,即使技术上可以这样做。取消转义后通过NSLog查看json字符串是什么。

标签: javascript ios objective-c json coffeescript


【解决方案1】:

这确实解决了问题。

url += encodeURIComponent JSON.stringify callInfo

【讨论】:

    【解决方案2】:

    在我看来,您正在尝试解析 url 字符串而不是解析 URL 内容。你确定这在模拟器中有效吗?

    您的代码:

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

    我认为应该是:

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

    【讨论】:

    • 当我尝试这样做时,它让我 WebKit 在 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: 数据参数中丢弃了一个未捕获的异常
    猜你喜欢
    • 2020-07-09
    • 2017-02-04
    • 2016-06-07
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2011-09-07
    相关资源
    最近更新 更多