gpt4 book ai didi

ios - 使用返回的 JSON 值设置 NSString

转载 作者:行者123 更新时间:2023-11-29 12:03:23 25 4
gpt4 key购买 nike

我正在尝试使用下面“值”(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com