iphone - 使用JSON框架时应用崩溃

标签 iphone json crash polling

这是我用来从json框架创建轮询数据的代码。但是,每次我启动该应用程序时,它都会崩溃。有什么建议吗?

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;

    NSArray *latestEvent = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Events"];
    [responseString release];

    //Choose event
    NSDictionary *flashMob = [latestEvent objectAtIndex:0];

    //Fetch the Data
    NSString *name = [flashMob objectForKey:@"event"];
    NSDate *eDate = [flashMob objectForKey:@"date"];
    NSString *location = [flashMob objectForKey:@"location"];
    NSString *danceVid = [flashMob objectForKey:@"dancevideo"];

    //Set the text to the label
    label.text = [NSString stringWithFormat:@"Next Flash Mob is: %@, on %@, near %@, with Dance: %@", name, eDate, location, danceVid];

}

Json是:
{ "Events":[{"id":0001,"event":"Party Rock Anthem Flash Mob","date":"12/18/2011",
"dancevideo":"http://www.youtube.com/watch?v=dP2ddTuMKIg","Location":"Flatirons Crossing,       Broomfield, CO"}]
}

编辑我还将包括viewDidLoad方法,除非调用可能失败。
 - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.corliosity.com/denverflash.json"]];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
        responseData = [[NSMutableData data] retain];

        // Do any additional setup after loading the view from its nib.
    }

最佳答案

在下面尝试此代码

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // try this code here
    NSDictionary *JSONDictionary = [[[[SBJsonParser alloc] init] autorelease] objectWithData:responseData];
    [self.responseData release] // only if is retained by your class.
    self.resposeDate = nil;

    NSArray *latestEvent = [JSONDictionary valueForKey:@"Events"];
    [responseString release];

    //Choose event
    NSDictionary *flashMob = [latestEvent objectAtIndex:0];

    //Fetch the Data
    NSString *name = [flashMob objectForKey:@"event"];
    NSDate *eDate = [flashMob objectForKey:@"date"];
    NSString *location = [flashMob objectForKey:@"location"];
    NSString *danceVid = [flashMob objectForKey:@"dancevideo"];

    //Set the text to the label
    label.text = [NSString stringWithFormat:@"Next Flash Mob is: %@, on %@, near %@, with Dance: %@", name, eDate, location, danceVid];
}

现在,您应该在JSONDictionary中看到Events键,相应的对象应该是字典数组。
以您喜欢的方式处理它。

关于iphone - 使用JSON框架时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8437630/

相关文章:

java - 安卓 : JNI crash

android - setText使我的应用程序崩溃-inStream.read

iphone - Google AdMob 不显示广告

iphone - 确定用户位置的合理时间长度 iPhone

iphone - iPhone 短信网关

java - 从具有单个/多个子项的 XML 进行 Json 转换

json - 如何仅显示不是 jq 1.3 版数组或对象的顶级 JSON 条目?

javascript - 使用 Ajax 的 jQuery Select2 插件

ios - AWS Cognito 数据集同步限制 - 每组同步超过 1mb 时会发生什么

c - 我的简单 C 程序崩溃了