我无法从我的json获取所有URL。此代码仅显示json的第一个URL

时间:2014-03-06 12:35:26

标签: ios json nsarray nsdictionary

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://www.xovak.com/json_logo.php"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSDictionary *Logos = [[[json valueForKey:@"logos"]objectAtIndex:0]mutableCopy];
    NSMutableArray *img = [[NSMutableArray alloc]init];

    for (id item in Logos) {
        [img addObject:[Logos objectForKey:@"image_file"]];
        NSLog(@"%@",img);
    }
}

4 个答案:

答案 0 :(得分:2)

您确定Logos不应该是NSArray吗?至少,这就是你的JSON对象的样子。

请改为:

NSURL *url = [NSURL URLWithString:@"http://www.xovak.com/json_logo.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *Logos = [[json valueForKey:@"logos"] mutableCopy];
NSMutableArray *img = [[NSMutableArray alloc]init];

for (NSDictionary *item in Logos) {
  [img addObject:[item objectForKey:@"image_file"]];
  NSLog(@"%@",img);
}

答案 1 :(得分:0)

你只在Logos中放了一个项目。另外,它不应该是NSMutableDictionary而是NSArray

另一方面,您在Logos 中循环的项目实际上是NSDictionary s。

所以,你的代码现在应该是:

    NSArray *Logos = [json valueForKey:@"logos"];
    NSMutableArray *img = [[NSMutableArray alloc]init];

    for (NSDictionary *item in Logos) {
        [img addObject:[item objectForKey:@"image_file"]];
        NSLog(@"%@",img);
    }

答案 2 :(得分:0)

从JSON响应中获取所有imagFile的另一种方法。请检查一下。

NSURL *url = [NSURL URLWithString:@"http://www.xovak.com/json_logo.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *img = [[NSMutableArray alloc]init];
NSArray *listOfURL = [[NSArray alloc] init];
NSArray *websiteDetails = (NSArray *) [json objectForKey:@"logos"];

for(int count=0; count<[websiteDetails count]; count++)
{
    NSDictionary *websiteInfo = (NSDictionary *) [websiteDetails objectAtIndex:count];
    NSString *imagefile = (NSString *) [websiteInfo objectForKey:@"image_file"];
    if([imagefile length]>0)
    {
        NSLog(@"Imagefile URL is: %@",imagefile);
        [img addObject:imagefile];
    }
}

listOfURL = img;


---------// Add This Method in TableView Cell For Row at IndexPath Method//-----------------

// Logic For Downloading Image From URL and Show in TableviewCell
//get a dispatch queue

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{

    NSURL *url = [NSURL URLWithString:[listOfURL objectAtIndex:indexPath.row]];
    NSData *image = [[NSData alloc] initWithContentsOfURL:url];

    //this will set the image when loading is finished
    dispatch_async(dispatch_get_main_queue(), ^{

        cell.imageview.image = [UIImage imageWithData:image];
    });
});

答案 3 :(得分:-1)

NSDictionary *Logos = [[[json valueForKey:@"logos"]objectAtIndex:0]mutableCopy];替换为NSArray *Logos = [[json valueForKey:@"logos"] mutableCopy];以获取所有对象。

相关问题