1

我正在使用 JSON 解析 iTunes RSS 提要,但遇到了问题。以下代码对于movieName 输出之一运行正常,但我仍然没有得到movieSummary 输出。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    feed = [allDataDictionary objectForKey:@"feed"];
    arrayOfEntry = [feed objectForKey:@"entry"];

    for (NSDictionary *dictionTitle in arrayOfEntry) {

        NSDictionary *title = [dictionTitle objectForKey:@"title"];
        NSString *labelTitle = [title objectForKey:@"label"];

        [arrayLable addObject:labelTitle];

        NSDictionary *summary = [dictionTitle objectForKey:@"summary"];
        NSString *labelSummary = [summary objectForKey:@"label"];

        [arraySummary addObject:labelSummary];
    }

    movieName.text = [arrayLable objectAtIndex:0];
    movieSummary.text = [arraySummary objectAtIndex:0]; //This is not displaying 
}

这是我正在解析的链接:http: //itunes.apple.com/us/rss/topmovies/limit=300/json

4

2 回答 2

0

问题是,当我将字符串添加到有时包含 NULL 的数组时,因此以下代码帮助了我

    if ([[arrayName objectAtIndex:0] isKindOfClass:[NSNull class]]) {
    labelName.text = @"This is NULL";
} else {
    [arrayName addObject:labelName];
}

    if ([[arraySummary objectAtIndex:0] isKindOfClass:[NSNull class]]) {
    labelSummary.text = @"This is NULL";
} else {
    [arraySummary addObject:labelSummary];
}
于 2013-03-16T22:13:35.263 回答
0

我经常遇到这种情况。我用这样的东西。替换你的代码

NSString *labelTitle = [title objectForKey:@"label"];
[arrayLable addObject:labelTitle];

NSString * labelTitle = [ [ title objectForKey:@"label" ] ifNullThenNil ] ;
[ arrayLabel addObject:labelTitle ? labelTitle : @"" ] ; // you could also use @"<unknown>" or similar instead of @""

其中-ifNullThenNil通过类别提供:

@implementation NSObject (IfNullThenNil)
-(id)ifNullThenNil { return self ; }
@end

@implementation NSNull (IfNullThenNil)
-(id)ifNullThenNil { return nil ; }
@end
于 2013-03-16T22:23:19.067 回答