0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · บทเรียน

การแยกวิเคราะห์ JSON ด้วย NSJSONSerialization

เปลี่ยนการตอบกลับจาก API ให้เป็นออบเจกต์ Objective-C ที่ใช้งานได้ด้วย NSJSONSerialization และเข้าถึงพจนานุกรมกับอาร์เรย์ผลลัพธ์อย่างปลอดภัย

การแยกวิเคราะห์ JSON ด้วย NSJSONSerialization เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.

คำถามที่พบบ่อย

บทเรียน “การแยกวิเคราะห์ JSON ด้วย NSJSONSerialization” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแยกวิเคราะห์ JSON ด้วย NSJSONSerialization” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. NSURLSession สำหรับการเรียก API
  2. Grand Central Dispatch (GCD)
  3. NSOperationQueue สำหรับงานที่ซับซ้อน
  4. การแยกวิเคราะห์ JSON ด้วย NSJSONSerialization
← กลับไปที่ Objective-C iOS Development for Legacy & Enterprise Apps