Objective-C: JSON Unrecognised leading character?

北慕城南 提交于 2019-12-08 13:51:01

问题


when trying to execute little function to get an array using JSON i get the next message:

-JSONValue failed. Error trace is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x5d50e30 {NSLocalizedDescription=Unrecognised leading character}"

This is the code

NSString * payloadAsString = [[NSString alloc] initWithData:receivedData 
                               encoding:NSUTF8StringEncoding];
NSLog(@"%@",payloadAsString);
NSMutableArray *jsonArray = [[NSMutableArray alloc] initWithArray:
                                 [payloadAsString JSONValue]];
self.loginArray = jsonArray;

NSLog prints [["True","1","MTY"]] (as far as i know it should work).

Any idea?


回答1:


I'm not sure which version of the json-framework you are using, but I modified the code to print the offending character:

In SBJSONParser.m:

- (BOOL)scanValue:(NSObject **)o
{
...
default:
[self addErrorWithCode:EPARSE description:[NSString stringWithFormat:@"Unrecognised leading character (%x)", c[-1]]];
//[self addErrorWithCode:EPARSE description: @"Unrecognised leading character"];
return NO;
}

You might try setting a breakpoint at that line of code instead.

Also, in general, you can try pasting the JSON into JSONLint at http://jsonlint.com/ to see if it validates. Your JSON does validate, though, so your problem is elsewhere.




回答2:


Make sure your string values are using a valid straight double quotation mark character. I've seen people try to copy-paste JSON out of an editor only to have their quotes turned into open-quote close-quote pairs like these:

“”




回答3:


I just had this problem and it turned out to be that the permissions on the directory used by the web service had been changed.



来源:https://stackoverflow.com/questions/6974863/objective-c-json-unrecognised-leading-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!