试图从Instagram解析简单的JSON数据,但是遇到了这个问题。JSON数据在应用程序中返回被截断的内容,但通过我的Mac上的浏览器,一切正常。
试图做很多不同的事情,但是都一样。
第一种方式:
NSURL *instaGetRecentOwnerPhotosURL = [NSURL URLWithString:@"https://api.instagram.com/v1/users/self/media/recent/?access_token=MY_PROPER_TOKEN"];
NSData *jsonData = [NSData dataWithContentsOfURL:instaGetRecentOwnerPhotosURL];
另一种方式,异步:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.instagram.com/v1/users/self/media/recent/?access_token=MY_PROPER_TOKEN"]];
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSLog(@"Async JSON: %@", json);
}];
JSON数据返回如下:截断的json的屏幕截图
绝对不知道出什么问题了。
它没有被截断。日志仅显示部分输出。如果它确实被截断了,那么它要么根本就不会被解析,要么只有更少的条目。但是数据确实解析了。没什么错json
。
顺便说一句-做适当的错误检查:
NSError *error = nil;
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (json) {
// Data is good. Work with 'json'
} else {
NSLog(@"Unable to parse JSON. Error: %@", error);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句