所以我仍在学习 iOS 和 JSON。
所以我认为我已经理解了基础知识(显然是一些东西):也许它是一个编码问题。
#define flickrGetURL [NSURL URLWithString:@"http://www.flickr.com/services/rest/?method=flickr.photos.search&tags=football&safe_search=1&per_page=20&format=json&api_key=YOURAPIREPLACESHERE"]
NSData* data = [NSData dataWithContentsOfURL:flickrGetURL];
NSError* error;
//this works
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response data being called : %@", stringData);
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(json == nil)
{
NSLog(@"NULL");
}else
NSLog(@" WORKED");
因此 URL 请求看起来不错,并且 String 打印出了完整的 JSON 对象。 但是我创建的 JSON 对象是 NILL,也许是编码问题。
与编码有关吗? (在非 Twitter JSON 页面上,此方法有效)。
最佳答案
我不确定尝试这个,
在代码中将 url 格式更改为此并检查
[NSURL URLWithString:[NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&text=%@&per_page=20&format=json&nojsoncallback=1",kFlickrAPIKey,searchTerm]];
where,searchTerm 字符串你要搜索的内容
关于iOS flickr 搜索照片 json 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19680636/