0

何らかの理由で、これは 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

それが違いを生んだ。

ありがとう。

4

2 に答える 2

0

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];
于 2013-08-16T23:49:41.900 に答える
0

これで問題は解決しました。

url += encodeURIComponent JSON.stringify callInfo
于 2013-08-27T23:10:01.560 に答える