1

我一直在尝试解析 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;
}

提前致谢!

4

2 回答 2

2

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

于 2013-03-17T07:56:05.690 回答
0

你可以做这样的事情

    for (int i = 0; i < [responseObject count]; i++) {
        NSLog([[responseObject objectAtIndex:i] objectForKey:@"description"]);
    }
于 2014-05-02T11:13:22.813 回答