我正在尝试使用下面“值”(secondLink) 中的 JSON 数据中返回的 url 来设置我的 imageView。当我使用以下代码时,它通过我的 XCode 控制台返回 JSON 中的整个 block - 如果我只希望返回图像的 URL,我的代码应该是什么样子(现在,我假设它正在尝试设置 imageView 返回的所有内容在第二个链接中)?
ViewController.m
NSString *secondLink = [[[[DIOSSession sharedSession] user] objectForKey:@"field_photo_path"] objectForKey:@"safe_value"];
NSLog(@"This is second link %@", secondLink);
if([secondLink length]>0) {
NSString *ImageURL = secondLink;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
self.imageView.image = [UIImage imageWithData:imageData];
} else {
NSString *ImageURL = @"defaulturl.com";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
self.imageView.image = [UIImage imageWithData:imageData];
}
JSON 数据
{
und = (
{
format = "<null>";
"safe_value" = "http://myurl.com/sites/default/files/stored/photo.jpg";
value = "http://myurl.com/sites/default/files/stored/photo.jpg";
}
);
}
最佳答案
在 field_photo_path
中有一个包含数组的字典 und
。
数组中的第一项是包含键 safe_value
的字典。
它假设user
也是一个字典(至少它响应objectForKey:
)
NSDictionary *user = [[DIOSSession sharedSession] user];
NSString *secondLink = user[@"field_photo_path"][@"und"][0][@"safe_value"];
关于ios - 使用返回的 JSON 值设置 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994455/