ios - 我在这个 JSON 解析中做错了什么? (iOS)

标签 ios json parsing

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

已关闭 9 年前

我想制作一个与 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/

上一篇:ios - XCODE 中的 JSON 解析不一致(Cocoa 错误 3840)

下一篇:ios - PhoneGap 3 BarcodeScanner 和未定义错误

相关文章:

javascript - 在元素名称发生变化的循环中解析 json

iphone - UITabBarController 图像 iOS 5

ios - 扩展匹配泛型的父类(super class)?

ios - 等距平铺 map 节点不出现

json - 如何从 Twitter 下载不同大小的图像?

python - 如何通过curl向couchDB中插入数百万文档?

ios - UIBezierPath绘图占用CPU 100%

javascript - 如何将 ng-options 和 ng-select 与 json 一起使用

c# - Convert.ToBoolean(string) 和 Boolean.Parse(string) 有什么区别?

javascript - 使用正则表达式从字符串中提取十六进制代码