简体   繁体   中英

How to parse JSON when dictionary has no title in iOS?

I have been trying to parse JSON, but I have been unable to because the source I am pulling from does not give me a dictionary title, leaving the array as the top value in the feed. How could I modify my code to parse the entire dictionary without the dictionary title? I have see this question asked in a few places, but the implementations do not match my need/ do not give me enough information to implement. Any help would be appreciated.

Where it says NSArray *array = [dictionary objectForKey:@""]; below, I am unable to give an object because my JSON code does not have a title. What should I do?

My code for parsing the dictionary:

- (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"]);
    }
}

My JSON Data:

[
  {
    "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
  }
]

My JSON Parsing code:

- (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;
}

Thanks in advance!

An array is a valid root object for JSON. Since that's the JSON you have, rewrite your code to expect an array of dictionaries instead. resultObject is a NSArray, not an NSDictionary (that's why JSONObjectWithData: returns an id ).

You could do something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM