我使用以下代码根据从服务器接收到的 JSON 字符串创建字典。(我已经下载了 JSONKit 并将其嵌入到项目中)。下面的代码从服务器返回一个合法的 JSON 字符串(在 Android 上解析得很好)但是当我尝试将它转换为字典时崩溃。
- (IBAction)submit
{
bool useSSL = true;
char *c_url="http://(rest of URL)";
NSString* url = [NSString stringWithFormat:@"%s" , c_url];
url = [NSString stringWithFormat:@"%@%@%s", url, self.label.text, "/keys"];
NSString * response = [self getDataFrom:url];
NSDictionary *dict = [response objectFromJSONString]; //generates SIGABRT!!
NSLog(@"%@",dict);
NSString *success = [dict valueForKey:@"success"];
}
- (NSString *) getDataFrom:(NSString *)url{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %i", url, [responseCode statusCode]);
return nil;
}
return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
}
谢谢,西蒙