Here’s the translation of the JSON example from Go to Objective-C, formatted for Hugo:
Our first program will demonstrate JSON encoding and decoding in Objective-C. Here’s the full source code:
This program demonstrates JSON encoding and decoding in Objective-C:
We start by encoding basic data types (boolean, integer, float, string) to JSON.
We then encode more complex structures like arrays and dictionaries (equivalent to slices and maps in Go).
We create a custom Response class and demonstrate how to encode it to JSON.
We show how to decode JSON data into Objective-C dictionaries and arrays.
Finally, we decode JSON data into our custom Response class.
To run the program, save it as json_example.m and compile it with:
This will output the JSON representations of various data types and structures.
Note that Objective-C uses NSJSONSerialization for JSON operations, which is part of the Foundation framework. The @autoreleasepool block is used for memory management.
Unlike Go, Objective-C doesn’t have built-in JSON tags for properties. Instead, you typically use key-value coding or custom serialization methods for more complex JSON mapping scenarios.