Objective-C iOS Development for Legacy & Enterprise Apps · درس

تحليل JSON باستخدام NSJSONSerialization

حوّل استجابات API إلى كائنات Objective-C قابلة للاستخدام باستخدام NSJSONSerialization، وتنقّل بأمان في القواميس والمصفوفات الناتجة.

الدرس 4 من 413 خطوة

تحليل JSON باستخدام NSJSONSerialization درس مجاني في Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Objective-C iOS Development for Legacy & Enterprise Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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:

  • NSJSONSerialization maps 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.

البدء مجانًا

تعلم Objective-C مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تحليل JSON باستخدام NSJSONSerialization» مجاني؟

نعم — نص درس «تحليل JSON باستخدام NSJSONSerialization» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Objective-C iOS Development for Legacy & Enterprise Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.

ماذا ستتعلم في «تحليل JSON باستخدام NSJSONSerialization»؟

حوّل استجابات API إلى كائنات Objective-C قابلة للاستخدام باستخدام NSJSONSerialization، وتنقّل بأمان في القواميس والمصفوفات الناتجة. تتمرن على Objective-C iOS Development for Legacy & Enterprise Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Objective-C iOS Development for Legacy & Enterprise Apps؟

لا تُشترط خبرة سابقة. Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تحليل JSON باستخدام NSJSONSerialization»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Objective-C iOS Development for Legacy & Enterprise Apps هذا؟

نعم. كل درس في Objective-C iOS Development for Legacy & Enterprise Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ‏NSURLSession لاستدعاءات API
  2. التوزيع المركزي الكبير (GCD)
  3. ‏NSOperationQueue للمهام المعقدة
  4. تحليل JSON باستخدام NSJSONSerialization
← العودة إلى Objective-C iOS Development for Legacy & Enterprise Apps