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なので、代わりに辞書の配列を期待するようにコードを書き直してください。resultObjectNSDictionary ではなく NSArray です (そのため、 JSONObjectWithData: は を返しますid)。

于 2013-03-17T07:56:05.690 に答える