简体   繁体   English

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

[英]i can't get all the URL from my json. this code show only first URL of json

- (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);
    }
}

Are you sure Logos shouldn't be a NSArray ? 您确定Logos不应该是NSArray吗? At least, that's what it looks like from your JSON object. 至少,这就是您的JSON对象的外观。

Do this instead: 改为这样做:

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);
}

You're putting only one item in Logos . 您仅在Logos放置一项。 Plus, it should not be NSMutableDictionary but an NSArray . 另外,它不应为NSMutableDictionary ,而应为NSArray

On the other hand, the items you're looping on in Logos are actually NSDictionary s. 另一方面,您在Logos 循环播放的项目实际上是NSDictionary

So, your code should now be: 因此,您的代码现在应为:

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

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

Another way of getting all imagFile From JSON Response. 从JSON响应中获取所有imagFile的另一种方法。 Please Check it. 请检查一下。

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];
    });
});

Replace NSDictionary *Logos = [[[json valueForKey:@"logos"]objectAtIndex:0]mutableCopy]; 替换NSDictionary *Logos = [[[json valueForKey:@"logos"]objectAtIndex:0]mutableCopy]; with NSArray *Logos = [[json valueForKey:@"logos"] mutableCopy]; 使用NSArray *Logos = [[json valueForKey:@"logos"] mutableCopy]; to get all objects. 获取所有对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM