Ios JSON返回空崩溃应用程序

Ios JSON返回空崩溃应用程序,ios,json,null,Ios,Json,Null,我有一些使用Dribble API返回快照的代码,但是,如果JSON中的任何数据为Null,应用程序就会崩溃,我已经尝试实现并捕捉到如果为Null,则继续,但这似乎不起作用。有人能给我建议吗 非常感谢 詹姆斯 您需要适当地处理连接错误。 试着这样做: self.JSONString = [NSString stringWithFormat:@"http://api.dribbble.com/shots/1970521"]; NSURLRequest *request = [[NSURLReque

我有一些使用Dribble API返回快照的代码,但是,如果JSON中的任何数据为Null,应用程序就会崩溃,我已经尝试实现并捕捉到如果为Null,则继续,但这似乎不起作用。有人能给我建议吗

非常感谢

詹姆斯


您需要适当地处理连接错误。 试着这样做:

self.JSONString = [NSString stringWithFormat:@"http://api.dribbble.com/shots/1970521"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.JSONString]];

__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {
        if (connectionError != nil || data == nil)
        {
            NSLog(@"Error loading dribble shot: %@",connectionError);
            //TODO: Show an alert to the user with UIAlertView?
        }
        else
        {
            NSError *jsonParsingError = nil;
            json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
            if (jsonParsingError != nil || json == nil){
                NSLog(@"Error parsing JSON: %@",jsonParsingError);
                //TODO: User alert?
            }
            else
            {
                NSLog(@"Parsed JSON: %@",json);
                //Process parsed JSON, update UI, etc.
                //Keep in mind updates to the UI should be done in a main-thread call like this:
                dispatch_sync(dispatch_get_main_queue(),^{
                    //Update UI here
                });
            }
        }
    }
}];

您可以使用第三方Dribble iOS SDK。它包装了所有的Dribble API,提供了单行API调用方法,还包括OAuth2集成的流和数据模型类。签出:

在处理
json
后,您的应用程序一定会崩溃,该值为
null
。解决方案是[NSJSONSerialization JSONObjectWithData:data options:0错误:nil];无法解析您的数据,原因可能是传递给它的非UTF数据。/questions/5716942/touchjson处理NSNull您应该将XCode调试器附加到应用程序并发布崩溃回溯,否则很难帮助您。是否引发异常?这是什么类型的崩溃?请注意,
NSJSONSerialization JSONObjectWithData
有一个未使用的错误参数。NSLog
error
if is结果为nil。您好,使用if(data!+nil)可以阻止我的应用程序现在崩溃,但如果我尝试在else{return;}之后对其进行NSLog,也会停止返回任何JSON
self.JSONString = [NSString stringWithFormat:@"http://api.dribbble.com/shots/1970521"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.JSONString]];

__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {
        if (connectionError != nil || data == nil)
        {
            NSLog(@"Error loading dribble shot: %@",connectionError);
            //TODO: Show an alert to the user with UIAlertView?
        }
        else
        {
            NSError *jsonParsingError = nil;
            json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
            if (jsonParsingError != nil || json == nil){
                NSLog(@"Error parsing JSON: %@",jsonParsingError);
                //TODO: User alert?
            }
            else
            {
                NSLog(@"Parsed JSON: %@",json);
                //Process parsed JSON, update UI, etc.
                //Keep in mind updates to the UI should be done in a main-thread call like this:
                dispatch_sync(dispatch_get_main_queue(),^{
                    //Update UI here
                });
            }
        }
    }
}];