user3509615
user3509615

Reputation: 1

How to parse image URL from json

"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],

this is my json data i can able to fetch data in text type but i can't able to fetch image url from weatherIconUrl array

Upvotes: 0

Views: 1538

Answers (2)

Satheesh
Satheesh

Reputation: 11276

Its quite easy,

 NSError *jsonParsingError = nil;

 NSArray *weatherArray = [NSJSONSerialization JSONObjectWithData:yourResponseData options:0 error:&jsonParsingError];

 NSLog(@"weatherArray : %@", weatherArray);

 NSString *weatherIconImageURL = [[weatherArray objectAtIndex:0] objectForKey:@"weatherIconUrl"]];

 NSLog(@"weatherIconImageURL : %@", weatherIconImageURL);

Upvotes: 0

iPatel
iPatel

Reputation: 47069

Use following code of line for get value of key @"value"

NSString * imgURLString = [[[myMaindictonary objectForKey:@"weatherIconUrl"] objectAtIndex:0] objectForKey:@"value"];

And convert this string to URL

NSURL *myURL = [NSURL URLWithString:[imgURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

And you can also get image from this URL by

UIImage *myImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:myURL]];

Upvotes: 2

Related Questions