How to do parsing from JSON

I have a project in which I have to insert an image from JSON. I am trying to find any tutorial about this, try to watch any video from this topic, but nothing. So I have a problem:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil]; NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist_data"]; for (NSDictionary *diction in playlist) { NSString *name = [diction objectForKey:@"text1"]; NSString *namesong = [diction objectForKey:@"text2"]; NSString *images = [diction objectForKey:@"image"]; NSLog(@"%@", images); [array addObject:text1]; [array2 addObject:text2]; } [[self tableTrack]reloadData]; } 

I added text 1 to the cell, and text 2 its work was perfect, but how to add the image to array 3 (the image in the cell in my TableView)? I also tried adding an image, but it does not work for me:

 NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:@"image"]]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *imageLoad = [[UIImage alloc] initWithData:imageData]; cell.imageView.image = imageLoad; 

Please help with my problem or maybe give some tutorial with parsing from JSON, also youtube doesn't have a perfect tutorial from JSON parsing. Thanks!

+6
source share
3 answers

Do not store multiple data sources such as array1 , array2 , array3 , etc. This is bad coding and will be confused when debugging / troubleshooting. Instead, maintain a single array with the information to display in a separate cell of the table view.

 for (NSDictionary *dict in playlist) { // array is single data source [array addObject:diction]; } 

Then, when assigning data in the table view cell,

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; } cell.text1 = [[array objectAtIndex:indexPath.row] objectForKey:@"text1"]; cell.text2 = [[array objectAtIndex:indexPath.row] objectForKey:@"text2"]; //For displaying image by lazy loading technique use SDWebImage. [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; // If no place holder image, use this way // [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:nil]; } 

As I mentioned in the comments on lazy loading images from URLs in a JSON response, use SDWebImage . The use is simple as shown above. Alternatively, you can implement lazy loading yourself by examining this sample code from Apple: LazyTableImages

Hope this helps!

+1
source

According to https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html and http://www.json.org/ you cannot store binary data in JSON (unless it is not in base64).

There seems to be a problem with your webdata , which is not valid JSON. Did you check what was set to @"image" in this dictionary during debugging? You can also use [NSJSONSerialization isValidJSONObject:webdata] to make sure your data is in order.

0
source
 //prepare your method here.. -(void)hitimageurl{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:replacephotostring]]; //set your image on main thread. dispatch_async(dispatch_get_main_queue(), ^{ if (_photosArray==nil) { _showImageView.image = [UIImage imageNamed:@"noimg.png"]; } else { [_showImageView setImage:[UIImage imageWithData:data]]; } }); }); 
0
source

Source: https://habr.com/ru/post/956462/


All Articles