2014-03-06 41 views
0
- (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); 
    } 
} 
+0

@Wain副本中你會JSON數據 – codercat

+0

@iDev瀏覽器中粘貼上面的網址,將它永遠是在那個環節?這個問題在未來會有多大用處,如果不是... – Wain

+0

是的,你說的是正確的,但提問者是我們社區的初學者,他不知道社區的概念。 @Wain – codercat

回答

0

讓所有imagFile從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]; 
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]; 
    }); 
}); 
+0

in if [session ..])條件會話是指什麼? – user3382223

+0

它只是我的單人班,我爲所有班級添加了所有方法。我的意思是隻檢查字符串是否爲NULL。謝謝指出。 – 2014-03-07 12:46:10

+0

非常感謝你.. – user3382223

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

這是正確的+1 – codercat

+0

非常感謝你... – user3382223

+0

但我可以在我的表格單元格中加載這個url列表... plz發送一些暗示.. – user3382223

-1

更換NSDictionary *Logos = [[[json valueForKey:@"logos"]objectAtIndex:0]mutableCopy];NSArray *Logos = [[json valueForKey:@"logos"] mutableCopy];獲得的所有對象。

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