在我找到一个可以学习的非常好的教程之前,我曾经在解析 json 时遇到了麻烦。当我构建自己的 web 服务来解析 json 时,我遵循了相同的步骤,但是现在当我尝试解析 twitters json-feed 时,我正在画空白。这是我的代码。
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* itemNumber = [json objectForKey:@"posts"]; //2
NSUInteger numObjects = [itemNumber count];
arrayTweets = [[NSMutableArray alloc] init];
arrayTimes = [[NSMutableArray alloc] init];
if (numObjects != 0) {
int i = 0;
do
{
NSDictionary* loan = [itemNumber objectAtIndex:i];
NSString* text = [(NSDictionary*)loan objectForKey:@"text"];
[arrayTweets addObject:text];
NSString *time = [(NSDictionary*)loan objectForKey:@"created_at"];
[arrayTweets addObject:time];
i++;
} while (i < numObjects);
} else {
NSLog(@"Nej");
}
[tableTweet performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
注意 "NSArray* itemNumber = [json objectForKey:@"帖子"];"。因为我的旧 json-feed 曾经看起来像这样......
{"status":"ok","count":4,"count_total":4,"pages":1,"posts":[{"id":58,"type":
它一直在工作,因为我之前有“帖子”:[,但现在有了 twitter 提要,它刚刚开始:
[{"created_at":"Mon Aug 06 19:16:42 +0000 2012","id":232555817835048961,"...
我不知道该怎么做。我意识到这很愚蠢,但除非有人向我解释,否则我认为我不会学习。
任何帮助表示赞赏!