当字典在iOS中没有标题时如何解析JSON?

Posted

技术标签:

【中文标题】当字典在iOS中没有标题时如何解析JSON?【英文标题】:How to parse JSON when dictionary has no title in iOS? 【发布时间】:2013-03-17 07:48:44 【问题描述】:

我一直在尝试解析 JSON,但我无法解析,因为我从中提取的源没有给我一个字典标题,将数组作为提要中的顶部值。如何修改我的代码以解析没有字典标题的整个字典?我在几个地方看到过这个问题,但是实现不符合我的需要/没有给我足够的信息来实现。任何帮助将不胜感激。

上面写着 NSArray *array = [dictionary objectForKey:@""];下面,我无法给出对象,因为我的 JSON 代码没有标题。我该怎么办?

我的字典解析代码:

- (void)parseDictionary:(NSDictionary *)dictionary


  NSArray *array = [dictionary objectForKey:@""];


    if (array == nil) 
        NSLog(@"Expected an array");
        return;
    

    for (NSDictionary *resultDict in array) 
        NSLog(@"start_time: %@, location: %@", [resultDict objectForKey:@"start_time"], [resultDict objectForKey:@"location"]);
    

我的 JSON 数据:

[
  
    "athletic_opponent": null,
    "campus": null,
    "class_fk": 0,
    "contact_person": null,
    "description": "March Break",
    "destination_address": null,
    "end_date": "2013-03-25",
    "end_time": null,
    "event_pk": 19603,
    "event_type": "No School",
    "game_outcome": "N/A",
    "game_score_opponent": 0,
    "game_score_us": 0,
    "google_directions_from_school": null,
    "google_map": null,
    "grade_level": "9, 10, 11, 12",
    "group_id": "0-2005-0-0-0-0-0-0",
    "group_pk": 0,
    "location": null,
    "notes": null,
    "primary_group": null,
    "school_level": "US",
    "start_date": "2013-03-22",
    "start_time": null,
    "student_group": null,
    "update_date": "2013-02-21",
    "url": null
  ,
  
    "athletic_opponent": null,
    "campus": null,
    "class_fk": 0,
    "contact_person": null,
    "description": "March Break",
    "destination_address": null,
    "end_date": "2013-03-25",
    "end_time": null,
    "event_pk": 19603,
    "event_type": "No School",
    "game_outcome": "N/A",
    "game_score_opponent": 0,
    "game_score_us": 0,
    "google_directions_from_school": null,
    "google_map": null,
    "grade_level": "9, 10, 11, 12",
    "group_id": "0-2005-0-0-0-0-0-0",
    "group_pk": 0,
    "location": null,
    "notes": null,
    "primary_group": null,
    "school_level": "US",
    "start_date": "2013-03-23",
    "start_time": null,
    "student_group": null,
    "update_date": "2013-02-21",
    "url": null
  ,
  
    "athletic_opponent": null,
    "campus": null,
    "class_fk": 0,
    "contact_person": null,
    "description": "March Break",
    "destination_address": null,
    "end_date": "2013-03-25",
    "end_time": null,
    "event_pk": 19603,
    "event_type": "No School",
    "game_outcome": "N/A",
    "game_score_opponent": 0,
    "game_score_us": 0,
    "google_directions_from_school": null,
    "google_map": null,
    "grade_level": "9, 10, 11, 12",
    "group_id": "0-2005-0-0-0-0-0-0",
    "group_pk": 0,
    "location": null,
    "notes": null,
    "primary_group": null,
    "school_level": "US",
    "start_date": "2013-03-24",
    "start_time": null,
    "student_group": null,
    "update_date": "2013-02-21",
    "url": null
  ,
  
    "athletic_opponent": null,
    "campus": null,
    "class_fk": 0,
    "contact_person": null,
    "description": "March Break",
    "destination_address": null,
    "end_date": "2013-03-25",
    "end_time": null,
    "event_pk": 19603,
    "event_type": "No School",
    "game_outcome": "N/A",
    "game_score_opponent": 0,
    "game_score_us": 0,
    "google_directions_from_school": null,
    "google_map": null,
    "grade_level": "9, 10, 11, 12",
    "group_id": "0-2005-0-0-0-0-0-0",
    "group_pk": 0,
    "location": null,
    "notes": null,
    "primary_group": null,
    "school_level": "US",
    "start_date": "2013-03-25",
    "start_time": null,
    "student_group": null,
    "update_date": "2013-02-21",
    "url": null
  ,
  
    "athletic_opponent": null,
    "campus": null,
    "class_fk": 0,
    "contact_person": null,
    "description": "Boarders back by 9:00",
    "destination_address": null,
    "end_date": null,
    "end_time": null,
    "event_pk": 19604,
    "event_type": "Other",
    "game_outcome": "N/A",
    "game_score_opponent": 0,
    "game_score_us": 0,
    "google_directions_from_school": null,
    "google_map": null,
    "grade_level": "9, 10, 11, 12",
    "group_id": "0-2005-0-0-0-0-0-0",
    "group_pk": 0,
    "location": null,
    "notes": null,
    "primary_group": null,
    "school_level": "US",
    "start_date": "2013-03-25",
    "start_time": null,
    "student_group": null,
    "update_date": null,
    "url": null
  
]

我的 JSON 解析代码:

- (NSDictionary *)parseJSON:(NSString *)jsonString

    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;

    id resultObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (resultObject == nil) 
        NSLog(@"JSON Error: %@", error);
        return nil;
    


    /*if (![resultObject isKindOfClass:[NSDictionary class]]) 
        NSLog(@"JSON Error: Expected dictionary");
        return nil;
    */


    return resultObject;

提前致谢!

【问题讨论】:

为什么需要 NSDictionary?如果你真的需要它,你可以从那个数组创建新的字典。 我被教导使用 NSDictionary 来解析 JSON,但我开始在自己的独立项目上工作,遇到了这个问题,不知道如何解决它。你会建议做什么而不是使用 NSDictionary? 您可以同时使用 NSArray 或 NSDictionary 作为 JSON 的根。如果使用数组,可以使用循环遍历 我想使用 NSArray,您对 parseDictionary 方法中的哪些更改有什么建议吗? - (void)parseDictionary:(NSArray *)array if (array == nil) NSLog(@"Expected an array");返回; for (NSDictionary *resultDict in array) NSLog(@"start_time: %@, location: %@", [resultDict objectForKey:@"start_time"], [resultDict objectForKey:@"location"]); 【参考方案1】:

数组是 JSON 的有效根对象。由于那是您拥有的 JSON,因此请重写您的代码以期望一个字典数组。 resultObject 是 NSArray,而不是 NSDictionary(这就是 JSONObjectWithData: 返回 id 的原因)。

【讨论】:

那么数组是根对象吗?我认为'[]'意味着字典是根对象。你会建议我如何修改我的代码来处理多个字典而不是数组? [] 表示数组, 表示字典。 无论如何,你的代码几乎就在那里,只需从 parseJSON 返回一个数组,然后将一个数组传递给 parseDictionary(也许重命名它!),然后从...部分中的 for 字典开始。 感谢您的帮助!我试过这个:NSArray *parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; parsedJSON = myJSON; 但是,我将如何更改 for (NSDictionary *resultDict in myJSON) NSLog(@"start_time: %@, location: %@", [resultDict objectForKey:@"start_time"], [resultDict objectForKey:@"location"]); ?我的调试面板说它应该“期望一个数组”。 这不是你自己的日志信息吗? parsedjson = MyJSON 的意义何在?这只会覆盖您提取的数组!【参考方案2】:

你可以这样做

    for (int i = 0; i < [responseObject count]; i++) 
        NSLog([[responseObject objectAtIndex:i] objectForKey:@"description"]);
    

【讨论】:

以上是关于当字典在iOS中没有标题时如何解析JSON?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ios 中解析这种类型的 JSON 响应? [复制]

解析 JSON 字典时调试器中的不同类型

iOS - 在 Swift 中使用 NSJSONSerialization 解析 JSON 字典

使用 Alamofire 解析 JSON 问题

解析 JSON 到字典,真假问题

如何在 Swift 5 中使用 Alamofire 解析 json