首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON-Framework时应用程序崩溃

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

Stack Overflow用户
提问于 2011-12-09 05:00:41
回答 4查看 154关注 0票数 0

下面是我用来从json框架创建投票数据的代码。然而,每次我启动应用程序时,它都会崩溃。有什么建议吗?

代码语言:javascript
复制
-(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是:

代码语言:javascript
复制
{ "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"}]
}

Edit I还包含viewDidLoad方法,除非调用可能失败。

代码语言:javascript
复制
 - (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.
    }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-09 05:31:40

尝试下面的代码

代码语言:javascript
复制
-(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里面看到了事件键,相应的对象应该是一个字典数组。以你喜欢的方式处理它。

票数 0
EN

Stack Overflow用户

发布于 2011-12-09 05:13:05

检查didReceiveResponse回调,解析头部和错误码。

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

当我尝试使用online json validator验证这个JSON字符串时,我发现了一个错误。

票数 0
EN

Stack Overflow用户

发布于 2011-12-09 05:35:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8437630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档