使用JSON-Framework时应用程序崩溃

时间:2011-12-08 21:00:41

标签: 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.
    }

4 个答案:

答案 0 :(得分:0)

检查didReceiveResponse回调并解析标题和错误代码。

我在JSON中没有看到任何问题。但是使用javascript的eval方法检查JSON格式。

当我尝试使用online json validator验证此JSON字符串时,我可以看到一个错误。

答案 1 :(得分:0)

请尝试以下代码

-(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键,相应的对象应该是一个字典数组。 以您喜欢的方式处理它。

答案 2 :(得分:0)

我通过验证程序http://jsonlint.com运行了您的JSON字符串,但它不起作用。期待您的ID的字符串,它正在获得一个数字。我会检查你从哪里获取数据,也许就是这么简单。

答案 3 :(得分:0)

除了破坏的JSON(如果您将“{id”值从0001更改为1"0001"它有效)我认为您的基本问题是这两行:

NSDate *eDate = [flashMob objectForKey:@"date"];
NSString *location = [flashMob objectForKey:@"location"];

您正在为NSDate类型的指针分配NSString。 (NSString没有自动转换为NSDate,即使它看起来像是日期。)这很可能是你的主要问题。

其次,您的location变量将为nil,因为您的数据的密钥为Location,但您的代码使用location并使用小写“L”。