JSON in objective c

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:

 

Encoding

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值