由於某些原因,這適用於運行iOS 6+的仿真器,但不適用於運行iOS 5+的實際iPad。當我在iPad上運行它時,它會給我這個錯誤(和其他錯誤)。實際設備上的iOS JSON問題
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)"}}
代碼以JSON字符串轉換成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
任何想法如何得到這個兩個模擬器和實際設備的工作,以JSON?
UPDATE
到目前爲止,我想我固定它。如果我這樣做了,這將被標記爲答案。我必須在咖啡腳本中執行以下操作:
url += encodeURIComponent JSON.stringify callInfo
這樣做有所不同。
謝謝。
_Yo dawg,我聽說你喜歡JSON._爲什麼你的JSON字符串中有JSON字符串?你的字符串的字符42是第一個被轉義的'''是在那個嵌入的JSON字符串中,所以這個問題顯然與你對這些數據的轉義無關。 –
@AlexWayne所以是coffeeScript或objective-c代碼中的問題? –
像objective-c代碼,因爲我只是在coffeescript.org做了一個測試,我可以在json中做JSON.stringify,然後把它們變成字符串,然後我可以正確地得到所有的回覆。 –