NSJSONSerialization ile JSON Ayrıştırma
API yanıtlarını NSJSONSerialization ile kullanılabilir Objective-C nesnelerine dönüştürün ve elde edilen sözlükler ile dizilerde güvenle gezinin.
NSJSONSerialization ile JSON Ayrıştırma, CoddyKit'te ücretsiz bir Objective-C iOS Development for Legacy & Enterprise Apps dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Objective-C iOS Development for Legacy & Enterprise Apps öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Objective-C iOS Development for Legacy & Enterprise Apps kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Sıkça Sorulan Sorular
“NSJSONSerialization ile JSON Ayrıştırma” dersi ücretsiz mi?
Evet — “NSJSONSerialization ile JSON Ayrıştırma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Objective-C iOS Development for Legacy & Enterprise Apps kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Objective-C iOS Development for Legacy & Enterprise Apps kursu toplamda 4 dersten oluşur.
“NSJSONSerialization ile JSON Ayrıştırma” dersinde ne öğreneceğim?
API yanıtlarını NSJSONSerialization ile kullanılabilir Objective-C nesnelerine dönüştürün ve elde edilen sözlükler ile dizilerde güvenle gezinin. Objective-C iOS Development for Legacy & Enterprise Apps ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Objective-C iOS Development for Legacy & Enterprise Apps öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Objective-C iOS Development for Legacy & Enterprise Apps, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“NSJSONSerialization ile JSON Ayrıştırma” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Objective-C iOS Development for Legacy & Enterprise Apps dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Objective-C iOS Development for Legacy & Enterprise Apps dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- API Çağrıları için NSURLSession
- Grand Central Dispatch (GCD)
- Karmaşık Görevler için NSOperationQueue
- NSJSONSerialization ile JSON Ayrıştırma