How to parse JSON response to show Image and Webview.

Try doing something like this. This is not the exact solution and you’ll have to work around your way.

NSString *link =  [NSString stringWithFormat:@"yourServiceURL"];
    NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:encdLink];
    NSData *response = [NSData dataWithContentsOfURL:url];
    NSError *error;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];

Now you have your data in the array and extract it out using objectForKey or valueForKey.

This is what I am doing to fetch images from JSON and showing in my app. Firstly get the array from the above code. Then extract out the image content like this :

_ImageArray = [jsonArray valueForKey:@"ContentImage"];

Now you have the image name in your ImageArray.Since you have only imageName,you’ll have to prepare the image URL in your code. Now to load the image in the ImageView, you need to do something like this.

NSURL *imageurl = [NSURL URLWithString:[_ImageArray objectAtIndex:indexPath.item]];
ImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];

This is for image. You can do it for the webview also. Some changes will be required as its a Webview. But it’ll be done.

Leave a Comment