JSON mit NSJSONSerialization parsen
Wandeln Sie API-Antworten mit NSJSONSerialization in nutzbare Objective-C-Objekte um und navigieren Sie sicher durch die daraus entstehenden Dictionaries und Arrays.
JSON mit NSJSONSerialization parsen ist eine kostenlose Objective-C iOS Development for Legacy & Enterprise Apps-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Objective-C iOS Development for Legacy & Enterprise Apps-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Objective-C iOS Development for Legacy & Enterprise Apps-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
JSON Meets Objective-C
Most APIs return JSON. Objective-C parses it with the Foundation class NSJSONSerialization, mapping JSON to native objects you already know.
The Type Mapping
JSON maps to Foundation types:
- object ->
NSDictionary - array ->
NSArray - string ->
NSString - number/bool ->
NSNumber - null ->
NSNull
Parsing Data to Objects
Call JSONObjectWithData:options:error: on the raw response data. It returns the top-level dictionary or array.
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data
options:0 error:&error];Always Check the Error
Malformed JSON returns nil and fills the error pointer. Check it before using the result.
if (!json) {
NSLog(@"Parse failed: %@", error.localizedDescription);
return;
}Reading Dictionary Values
Once parsed, access fields by key. Cast to the expected type for clarity.
NSDictionary *user = json;
NSString *name = user[@"name"];
NSNumber *age = user[@"age"];Iterating JSON Arrays
For a list response, loop the array and pull fields from each element.
NSArray *items = json[@"items"];
for (NSDictionary *item in items) {
NSLog(@"%@", item[@"title"]);
}Defensive Type Checks
APIs lie. Verify a value's class before using it to avoid crashes from unexpected shapes.
if ([json isKindOfClass:[NSDictionary class]]) {
// safe to treat as a dictionary
}Handling NSNull
JSON null becomes NSNull, not nil. Sending messages to it crashes, so check for it explicitly.
id value = user[@"nickname"];
if (value == [NSNull null]) { value = nil; }Encoding Back to JSON
To send JSON, go the other way with dataWithJSONObject:.
NSDictionary *body = @{@"name": @"Ada"};
NSData *out = [NSJSONSerialization dataWithJSONObject:body
options:0 error:nil];Mapping to Model Objects
Raw dictionaries are error-prone everywhere. A common pattern wraps parsing in a model's initializer, so the rest of the app uses typed objects instead of stringly-typed keys.
- (instancetype)initWithDictionary:(NSDictionary *)d {
self = [super init];
if (self) { _name = d[@"name"]; }
return self;
}Parse Off the Main Thread
Network responses arrive on a background thread. Parse there, then dispatch UI updates back to the main thread to keep the app responsive.
Quick Check
Test your JSON parsing knowledge.
Recap
You learned JSON parsing in Objective-C:
NSJSONSerializationmaps JSON to Foundation types- Always check the error and the result's class
- JSON null becomes
NSNull, not nil - Encode back with
dataWithJSONObject: - Map dictionaries into typed model objects
Defensive parsing keeps networking code crash-free.
Häufig gestellte Fragen
Ist die Lektion „JSON mit NSJSONSerialization parsen“ kostenlos?
Ja — der vollständige Text von „JSON mit NSJSONSerialization parsen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Objective-C iOS Development for Legacy & Enterprise Apps-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Objective-C iOS Development for Legacy & Enterprise Apps-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „JSON mit NSJSONSerialization parsen“?
Wandeln Sie API-Antworten mit NSJSONSerialization in nutzbare Objective-C-Objekte um und navigieren Sie sicher durch die daraus entstehenden Dictionaries und Arrays. Du übst Objective-C iOS Development for Legacy & Enterprise Apps mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Objective-C iOS Development for Legacy & Enterprise Apps zu starten?
Keine Vorkenntnisse erforderlich. Objective-C iOS Development for Legacy & Enterprise Apps auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „JSON mit NSJSONSerialization parsen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Objective-C iOS Development for Legacy & Enterprise Apps-Lektion Code schreiben und ausführen?
Ja. Jede Objective-C iOS Development for Legacy & Enterprise Apps-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- NSURLSession für API-Aufrufe
- Grand Central Dispatch (GCD)
- NSOperationQueue für komplexe Aufgaben
- JSON mit NSJSONSerialization parsen