<分区>
我想制作一个与 API 通信的应用程序,但我无法找出我的解析出了什么问题。
代码如下:
NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@" jsonObject: %@", jsonObject);
NSURL *img1 = [[NSURL alloc] initWithString:[jsonObject obj]];
NSURL *img2 = [[NSURL alloc] initWithString:[jsonObject valueForKey:@"img2"]];
NSURL *img3 = [[NSURL alloc] initWithString:[jsonObject valueForKey:@"img3"]];
NSData *data1 = [[NSData alloc] initWithContentsOfURL:img1];
NSData *data2 = [[NSData alloc] initWithContentsOfURL:img2];
NSData *data3 = [[NSData alloc] initWithContentsOfURL:img3];
UIImage *image1 = [[UIImage alloc] initWithData:data1];
UIImage *image2 = [[UIImage alloc] initWithData:data2];
UIImage *image3 = [[UIImage alloc] initWithData:data3];
这是树:
jsonObject: (
{
img1 = "http://www.kozpontiszalon.hu/images/ads/kupon_aug_3.png";
img2 = "anotherlink";
img3 = "anotherlink";
}
)
谢谢你帮助我!
最佳答案
你的第一行应该是:
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
因为您的 JSON 数据是一个数组。
该数组有一个包含图像 URL 的字典。
NSDictionary *urls = jsonObject[0];
NSURL *img1 = [[NSURL alloc] initWithString:urls[@"img1"]];
关于ios - 我在这个 JSON 解析中做错了什么? (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283098/