Objective-C 的 JSON 返回错误“Illegal start of token [r]"; [英] JSON for Objective-C returns error "Illegal start of token [r]"
问题描述
我正在尝试连接到网页并返回一个 JSON 字符串.这个字符串只需要在 JSON 中解析并返回到我等待它的 NSArray 中.问题是 JSON 并不总是返回结果.有时,它运作良好.有时,它返回 (null),引用下面的错误.
I am attempting to connect to a web page and return a JSON string. This string simply needs to be parsed in JSON and returned into an NSArray that I have waiting for it. The problem is that JSON doesn't always return the results. Sometimes, it works well. Sometimes, it returns (null), citing the error below.
self.accounts = nil; // Clear the NSArray
NSString *post = [NSString stringWithFormat:@"username=%@", username.text];
NSData *postData = [NSData dataWithBytes: [post UTF8String] length: [post length]];
// Submit login data
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString: @"http://###.#####.###/app/getaccts.php"]];
[request setHTTPMethod: @"POST"];
[request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody: postData];
// Retreive server response
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
accounts = [[content JSONValue] allValues]; // Parse JSON string into the array
NSLog(@"Array: %@", accounts);
我提交的页面返回这个:
The page I am submitting to returns this:
{"1":"856069060", "2":"856056407"}
JSON 记录以下错误:
JSON logs the following error:
-JSONValue failed. Error is: Illegal start of token [r]
推荐答案
我可以获取更多信息吗...具体来说,我希望您检查 sendSynchronousRequest 的返回值,以便您可以进行此更改并重新运行你的测试:
Can I get a little more info ... specifically, I'd like you to check the return value of your sendSynchronousRequest so can you make this change and rerun your test:
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (returnData == nil) {
NSLog(@"ERROR: %@", err);
} else {
NSLog(@"DATA: %@", returnData);
}
这篇关于Objective-C 的 JSON 返回错误“Illegal start of token [r]";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!