我正在尝试让NSLog打印出JSON数据。但它不会显示在日志中。我是Objective-C的新手。代码肯定出了什么问题?
@implementation demo2ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
title = @"title";
thumbnail = @"thumbnail";
author = @"author";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@""]];
// note : i have the ip address, but stackoverflow wont allow me to post it. so i deleted.
NSArray* jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:@"title"];
NSString *thumbnail_data = [dataDict objectForKey:@"thumbnail"];
NSString *author_data = [dataDict objectForKey:@"author"];
NSLog(@"TITLE: %@",title_data);
NSLog(@"THUMBNAIL: %@",thumbnail_data);
NSLog(@"AUTHOR: %@",author_data);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
title_data, title,
thumbnail_data, thumbnail,
author_data,author,
nil];
[myObject addObject:dictionary];
}
}
[ {
title: "Post From Wordpress Title",
content: "Some Content from Post on Wordpress",
author: "Banyapon Poolsawasd", thumbnail: ""
},{
title: "Post From Wordpress Title",
content: "Some Content from Post on Wordpress",
author: "Banyapon Poolsawasd",
thumbnail: ""
} ]您的帮助我们将不胜感激。谢谢。
发布于 2014-02-05 12:39:40
尝试通过NSLog连接到jsonSource
NSData *jsonSource = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@""]]; //Here you are not call url string
NSlog(@"jsonSource:%@", jsonSource);jsonSource将返回nil
NSArray* jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
NSlog(@"jsonObjects:%@", jsonObjects);jsonObjects也将返回nil
因此,控制器不会进入for循环,这就是不打印slog的原因。
发布于 2014-02-05 13:22:54
你的数据:
[ {
title: "Post From Wordpress Title",
content: "Some Content from Post on Wordpress",
author: "Banyapon Poolsawasd", thumbnail: ""
},{
title: "Post From Wordpress Title",
content: "Some Content from Post on Wordpress",
author: "Banyapon Poolsawasd",
thumbnail: ""
} ]在上面的例子中,你的数据是从NSDictionary.That开始的,而不是NSArray,是什么都没有显示的原因。
使用下面的代码获取标题、和其他值,如下所示。
NSLog(@"Title :%@",[[jsonObjects objectAtIndex:0]objectForKey:@"title"]);https://stackoverflow.com/questions/21568594
复制相似问题