2013-09-03 1 views
0

Я новичок в iOS, и я уже занимаюсь поиском в Google, но я не получил помощь в этом. Пожалуйста, помогите мне решить этот разбор JSON и как показать это изображение в UICollectionView.как разобрать этот json и показать в uicollectionview

{ 
    "wallpaper": [ 
     { 
      "id": "31", 
      "category": "animal", 
      "title": "demo", 
      "images": { 
       "image_thumb": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_thumb.jpg", 
       "image1": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_4.jpg", 
       "image2": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_5.jpg", 
       "image3": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_ipad.jpg", 
       "image4": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_android.jpg" 
      }, 
      "hits": "0" 
     }, 
     { 
      "id": "33", 
      "category": "abstract", 
      "title": "demo 2", 
      "images": { 
       "image_thumb": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beach1378075849_thumb.jpg", 
       "image1": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beach1378075849_4.jpg", 
       "image2": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beautifulnaturewallpapersforbackgroundhdwallpaper1378075850_5.jpg", 
       "image3": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/DragonflyWallpaperRainForDekstopHD1378075850_ipad.jpg", 
       "image4": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/hdwallpapers1080pwallpapers1378075850_android.jpg" 
      }, 
      "hits": "0" 
     }, 
} 
} 
+0

У вас есть обои в виде массива, который содержит несколько словарей .. и каждый dictnoary conatain идентификатора в качестве строки, категория строковой, название как строки, ударяется в виде строки и массива изображений , –

+1

На мой взгляд, вы должны начать здесь: http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/ – lvp

+0

вы ищете мой код .... – Jitendra

ответ

1

Надеется, что это помогает ;-)

//Let's asume your json is loaded in this variable 
NSString * myJsonString = @""; 

//convert string to dict 
NSData * data = [myJsonString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError * error; 
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error]; 

//Load array wallpapers (array of dictionaries) 
NSArray * myArray = [dict objectForKey:@"wallpaper"]; 

Теперь вы должны создать UITableViewCell (назовем его CustomCell) с желаемый дизайн, включая UIImageView для отображения изображения.

Тогда в TableView DataSource:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [myArray count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil]; 
     cell = (CustomCell *)temporaryController.view; 
    } 

    NSDictionary * dict = [myArray objectAtIndex:i]; 

    NSString * image_thumb_url = [[dict objectForKey:@"images"] objectForKey:@"image_thumb"]; 

    //todo: download the image and display it into your cell here 

    return cell; 

} 
1
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.0.5/getappdata.php"]]; 
NSData * data=[NSData dataWithContentsOfURL:url]; 

NSError * error; 

//Get json data in Dictionary 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error]; 

NSLog(@"%@",json); 

попробовать этот код для разбора JSON ...

+0

спасибо за быстрый повтор [ply am geting json responce, но когда я пытаюсь преобразовать объект изображения, строка reponce преобразуется в image_thumb "=" http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_thumb.jpg "; " image1 "=" http : //192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_4.jpg "; " image2 "=" http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_5.jpg "; " image3 "=" http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_ipad.jpg "; –

 Смежные вопросы

  • Нет связанных вопросов^_^