将 JSON url 图片解析到 iOS 不起作用
Posted
技术标签:
【中文标题】将 JSON url 图片解析到 iOS 不起作用【英文标题】:Parsing JSON url pictures to iOS not working 【发布时间】:2013-11-09 13:07:59 【问题描述】:我正在尝试解析包含图像链接的 json 文件。这是我的代码:
#import "Pictures.h"
#import "DEMONavigationController.h"
#import "PicturesObject.h"
@interface Pictures ()
NSInteger refreshIndex;
NSArray *images;
NSMutableArray *jsonIs;
NSArray *items;
IBOutlet UIImageView *imagesinsta;
@end
@implementation Pictures
- (void)viewDidLoad
[super viewDidLoad];
self.title = @"Pictures";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
style:UIBarButtonItemStylePlain
target:(DEMONavigationController *)self.navigationController
action:@selector(showMenu)];
// ***************** FETCHING DATA ******************* //
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"link-to-json.php?image=Image"]];
NSData *data= [NSData dataWithContentsOfURL:URL];
if (data == nil)
return;
NSError* error;
items = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSLog(@"Json : %@",jsonIs);
if (jsonIs != nil)
NSMutableDictionary* aDict = jsonIs[0];
NSString *item_media = [aDict objectForKey:@"link"];
// ***************** FETCHING DATA ******************* //
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"PicturesObject";
PicturesObject *cell = (PicturesObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PicturesObject" owner:self options:nil];
cell = [nib objectAtIndex:0];
// The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
NSDictionary *item = [items objectAtIndex:indexPath.row];
NSString *item_media = [item objectForKey:@"link"];
return cell;
- (void)issueLoadRequest
// Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"link-to-json.php?image=Image"]];
[self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
);
- (void)receiveData:(NSData *)data
// When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
// going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.tweets.count;
@end
这就是我的自定义 PicturesObject 表视图的样子:
但是当我启动我的应用程序时,我只会看到这样的屏幕:
【问题讨论】:
你能粘贴 JSON 响应吗? 在 NSLog 我得到: Json : (null) @Ernaidu 不,伙计,我问的是你在数据变量中获得的数据。尝试将 NSData 更改为 NSString 【参考方案1】:Json Data 类似于 Dictionary 而不是数组。而不是使用
NSArray items
使用
NSDictionary items
然后从字典中提取相关数据。
【讨论】:
好的,听起来像是一个解决方案。你能帮我写代码吗? @vaibhav_15 jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSArray *items = [jsonDict objectForKey:@"Links"]; 谢谢。这段代码应该在 viewDidLoad 方法中,还是? @vaibhav_15 使用此代码,我的应用程序崩溃并向我发送此错误:由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[__NSArrayM objectForKey:]: unrecognized selector sent to instance Ya viewDidLoad 我猜会很好。试试看,如果它有效,你也可以投票给答案!!;)以上是关于将 JSON url 图片解析到 iOS 不起作用的主要内容,如果未能解决你的问题,请参考以下文章