JSON is a light way to exchange data compared with XML. However, data in JSON does not have any meta data definition(e.g., schema) like XSD in XML. So it's diffcult to communicate object of complex definition and data cannot be validated since lack of schema.
The type mapping between JSON and objective c is claimed as:
NSDictionary instances are encoded into JSON Objects. NSDictionary can use non-strings as keys, but JSON does not allow this. Therefore, attempting to convert an NSDictionary with non-string keys into JSON will throw an exception.
Objective-C does not have a proper Boolean class type. NSNumber instances created with the +numberWithBool: are therefore turned into true or false in JSON.
NSNumber instances (other than the aforementioned ones created with +numberWithBool: ) map directly to Number objects in JSON.
Decoding
NSDictionary instances are encoded into JSON Objects. NSDictionary can use non-strings as keys, but JSON does not allow this. Therefore, attempting to convert an NSDictionary with non-string keys into JSON will throw an exception.
Objective-C does not have a proper Boolean class type, so a little trickery is required. JSON true or false values are represented by NSNumber instances created with the +numberWithBool: method.
In order to faithfully represent arbitrary-precision numbers, JSON numbers are turned into NSDecimalNumber instances.
NOTE: The object arrary returned by PHP is mapped to NSDictionary, in which each pair the key is array index and value is the actual object. However, there is an exception that when the object array is empty, it will be resolved as NSArray instead of NSDictionary. So it should be careful when enumerate the JSON decoded data:
Just simply enumerate the "collection" returned by JSON and get the value within the loop. This loop will be jumped out when there is no entry in the collection:
Important: All contents inside[] are encapsulated into arrays and all contents inside{} are encaped into diciionary.