如何从json解析图像URL

时间:2014-04-11 05:51:51

标签: ios objective-c json parsing

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

这是我的json数据,我能够以文本类型获取数据,但我无法从weatherIconUrl数组中获取图像网址

2 个答案:

答案 0 :(得分:2)

使用以下代码行获取密钥@"value"

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

并将此字符串转换为网址

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

您还可以通过

从此网址获取图片
UIImage *myImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:myURL]];

答案 1 :(得分:0)

很容易,

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