ios - 使用 AFNetworking 2.5 将 JSON 序列化为 NSArray

标签 ios objective-c json afnetworking afnetworking-2

到目前为止,利用 AFNetworking 2.5 通过 last.fm API 来填充与热门轨道相关的艺术家的数组一直是一个挑战。这是我当前执行此操作的代码。

NSURL *url = [NSURL URLWithString:URLstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *dict = (NSDictionary *)responseObject;
        NSArray *allValues = [dict allValues];
        NSLog(@"all values contained in array: %@", allValues);
        NSLog(@"all values count: %lu", allValues.count);

计数返回 1。我想要一个填充有轨道的数组,并且每个索引值对应于一个轨道。目前,数组中仅填充第一个索引值。我还注意到打印回的 JSON 数据是 JSON 格式的,并且不像打印时出现的 NSArray 中的对象那样。

编辑:这是 input

最佳答案

使用KVC获取轨道数组:

NSArray* tracks = [dict valueForKeyPath:@"toptracks.track"] 

或者类似的东西

关于ios - 使用 AFNetworking 2.5 将 JSON 序列化为 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27284696/

相关文章:

ios - 更改未在 Interface Builder 中创建的 UIButton 的文本

iphone - 如何在 iOS 上旋转自定义启动画面?

objective-c - Fading UIView 允许 subview 可见

objective-c - 用于 Cocoa 应用程序的 USB 信用卡读卡器

javascript - For in Javascript 不适用于来自 JSON 的事件

ios - Appcenter Push iOS - 控制何时请求推送权限

ios - GCM 证书配置不起作用或 iOS

ios - 我可以让 UITextField 不可见吗?

java - 安卓到休息: cannot serialize java Date

json - 如何向我的 Spring MVC REST 服务添加错误?