简体   繁体   English

如何在Objective-C中捕获JSON错误?

[英]How to catch JSON Error in Objective-C?

Follow this tutorial: http://www.raywenderlich.com/5492/working-with-json-in-ios-5 , I make simple App like that: 遵循本教程: http : //www.raywenderlich.com/5492/working-with-json-in-ios-5 ,我制作了一个简单的应用程序,如下所示:

#define kLatestKivaLoansURL [NSURL URLWithString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
        NSError* error;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                                                             options:kNilOptions
                                                               error:&error];
        NSArray* latestLoans = [json objectForKey:@"loans"];
        NSLog(@"Error: %@",error);
        NSLog(@"loans: %@",latestLoans); 

        dispatch_async(dispatch_get_main_queue(), ^(){
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            self.window.rootViewController = self.viewController;
            [self.window makeKeyAndVisible];
        });

    });

    return YES;
}

@end

When Network not OK or JSON link Error, I get same break: " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil" 当网络不正常或JSON链接错误时,我得到相同的中断:“ *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'数据参数为nil'

How to catch this Error? 如何捕获此错误? I just want to display an Alert message, not break. 我只想显示一个警报消息,而不是中断。

How many kind of Errors when parse JSON data? 解析JSON数据时会发生几种错误?

You should implement a check on NSData* data whether it is nil or not? 您应该检查NSData* data是否为nil? If its nil then you should not run the code line 如果为零,则不应运行代码行

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                                                             options:kNilOptions
                                                               error:&error];

as you are trying to convert nil data into Dictionary . 当您尝试将nil数据转换为Dictionary

You can also check network availability by implementing Reachability classes in you code. 您还可以通过在代码中实现Reachability类来检查网络可用性。 A sample application demonstrates how to use the SystemConfiguration framework to monitor the network state of an iPhone or iPod touch 一个示例应用程序演示了如何使用SystemConfiguration框架来监视iPhone或iPod touch的网络状态。

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM