Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AFNetworking将JSON解析到NSDictionary中_Ios_Objective C_Json_Afnetworking - Fatal编程技术网

Ios 使用AFNetworking将JSON解析到NSDictionary中

Ios 使用AFNetworking将JSON解析到NSDictionary中,ios,objective-c,json,afnetworking,Ios,Objective C,Json,Afnetworking,我试图用AFNetworking和NSDictionary解析一些JSON。然而,JSON似乎有些奇怪。JSON包含穿梭机的路由,但我在JSON中没有看到“路由” NSArray *routes = (NSArray *)responseObject; NSDictionary *oneRoute = responseObject[0]; NSDictionary *stops = oneRoute[@"Stops"]; 当我运行这段代码时,我得到一个空的NSDict

我试图用AFNetworking和NSDictionary解析一些JSON。然而,JSON似乎有些奇怪。JSON包含穿梭机的路由,但我在JSON中没有看到“路由”

    NSArray *routes = (NSArray *)responseObject;

    NSDictionary *oneRoute = responseObject[0];
    NSDictionary *stops = oneRoute[@"Stops"];
当我运行这段代码时,我得到一个空的NSDictionary,其中有15个分配的空间

NSString *methodURL = [NSString stringWithFormat:@"%@GetRoutesForMapWithSchedule", BASE_URL];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:methodURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    _routes = (NSDictionary *)responseObject;
    //_route = _routes[@"Stops"];

    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
有人能解释一下我如何获得一条路线的所有信息吗?是JSON

    NSArray *routes = (NSArray *)responseObject;

    NSDictionary *oneRoute = responseObject[0];
    NSDictionary *stops = oneRoute[@"Stops"];

问题是顶级JSON对象实际上是一个数组,而不是您试图将其转换为的字典。另一方面,数组的每个元素都是一个字典

下面是一些代码,用于获取一般数据结构并从其中一条路由中获取一段数据

NSArray *routes = (NSArray *)responseObject;

NSDictionary *firstRoute = responseObject[0];
NSString *description = firstRoute[@"Description"];
编辑:要分析
停止
,您可以执行以下操作:

NSArray *stops = firstRoute[@"Stops"];
NSDictionary *firstStop = stops[0];
NSString *stopDescription = firstStop[@"Description"];

你错过了模特课。您通常会使用该类将json数据序列化到您构建的模型类中。您可以详细说明吗?@jakepeterson这与问题无关。他可能有一个模型类,他没有发布,因为它不相关。@jakepeterson-不是在Objective-C中,你不会。找一个在线JSON格式化程序来格式化/缩进JSON,这样你就可以理解它。我尝试了所有步骤,但似乎做得不对,我更新了我的问题你能看一下吗?“stops”也是一个数组,许多字典。更新我的答案,向您展示如何解析它。您需要查看JSON的结构,以了解其中包含哪些类型的对象。方括号(
[]
)表示数组,而具有由冒号(
)分隔的键值对的弯曲括号(
{}
)是字典。