JSON的序列化
(一)序列化和反序列化
- 序列化 : 將
字典或者數(shù)組等OC對(duì)象轉(zhuǎn)換成二進(jìn)制數(shù)據(jù)準(zhǔn)備發(fā)送給服務(wù)器. - 反序列化 : 從服務(wù)器接收到
二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成字典或者數(shù)組等OC對(duì)象.
(二)JSON序列化使用場(chǎng)景
向服務(wù)器發(fā)送一些信息時(shí),比如,發(fā)微博...
我們可以將描述信息定義成JSON字符串,字典或者數(shù)組,再將其序列化成二進(jìn)制形式的JSON字符串,發(fā)送給服務(wù)器,服務(wù)器接收到之后,可以直接反序列化成JSON字符串,字典或者數(shù)組.
當(dāng)我們需要向服務(wù)器發(fā)送一個(gè)OC對(duì)象時(shí),可以將OC對(duì)象轉(zhuǎn)換成二進(jìn)制形式的JSON字符串.因?yàn)榉?wù)器不認(rèn)識(shí)OC對(duì)象,不能直接發(fā)送.
(三)序列化字典和數(shù)組
1.發(fā)送JSON數(shù)據(jù)到服務(wù)器
- 方案一 : 把JSON格式的字符串序列化成JSON的二進(jìn)制
#pragma 方案一 : 把JSON格式的字符串序列化成JSON的二進(jìn)制
- (void)POSTJSON_01
{
NSString *jsonStr = @"{\"name\":\"大發(fā)明家\"}";
// 把JSON格式的字符串序列化成JSON的二進(jìn)制
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
[self postJsonWith:jsonData];
}
- 方案二 : 把字典序列化成JSON格式的二進(jìn)制
#pragma 方案二 : 把字典序列化成JSON格式的二進(jìn)制
- (void)POSTJSON_02
{
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"亞索" forKey:@"name"];
// 把字典序列化成JSON格式的二進(jìn)制
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
[self postJsonWith:jsonData];
}
- 方案三 : 把數(shù)組序列化成JSON格式的二進(jìn)制
#pragma 方案三 : 把數(shù)組序列化成JSON格式的二進(jìn)制
- (void)POSTJSON_03
{
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"牛頭" forKey:@"name"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"石頭人" forKey:@"name"];
NSArray *arr = @[dict1,dict2];
// 把數(shù)組序列化成JSON格式的二進(jìn)制
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:0 error:NULL];
[self postJsonWith:jsonData];
}
2.發(fā)送json數(shù)據(jù)到服務(wù)器的主方法,傳入json數(shù)據(jù)的二進(jìn)制
#pragma 發(fā)送json數(shù)據(jù)到服務(wù)器的主方法,傳入json數(shù)據(jù)的二進(jìn)制
- (void)postJsonWith:(NSData *)jsonData
{
// URL
NSURL *URL = [NSURL URLWithString:@"http://localhost/php/upload/postjson.php"];
// 請(qǐng)求
NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:URL];
// 設(shè)置請(qǐng)求方法
requestM.HTTPMethod = @"POST";
// 設(shè)置請(qǐng)求體
requestM.HTTPBody = jsonData;
// 發(fā)送請(qǐng)求
[[[NSURLSession sharedSession] dataTaskWithRequest:requestM completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 處理響應(yīng)
if (error == nil && data != nil) {
// 反序列化
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
} else {
NSLog(@"%@",error);
}
}] resume];
}
(四)序列化自定義對(duì)象
1.準(zhǔn)備Person類(lèi)
- Person類(lèi)
.h文件
@interface Person : NSObject
/// 姓名
@property (nonatomic,copy) NSString *name;
@end
- Person類(lèi)
.m文件
@implementation Person {
/// 年齡
NSString *_age;
}
@end
- 方案四 : 自定義對(duì)象序列化成JSON格式的二進(jìn)制
#pragma 方案四 : 自定義對(duì)象序列化成JSON格式的二進(jìn)制
- (void)POSTJSON_04
{
Person *p = [[Person alloc] init];
// 給對(duì)象屬性或者私有成員變量賦值
p.name = @"張小廚zxc";
[p setValue:@"18" forKey:@"_age"];
// 先把對(duì)象轉(zhuǎn)成字典
NSDictionary *dict = [p dictionaryWithValuesForKeys:@[@"name",@"_age"]];
// 再把字典序列化成JSON格式的二進(jìn)制
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
[self postJsonWith:jsonData];
}
2.發(fā)送json數(shù)據(jù)到服務(wù)器的主方法,傳入json數(shù)據(jù)的二進(jìn)制
#pragma 發(fā)送json數(shù)據(jù)到服務(wù)器的主方法,傳入json數(shù)據(jù)的二進(jìn)制
- (void)postJsonWith:(NSData *)jsonData
{
// URL
NSURL *URL = [NSURL URLWithString:@"http://localhost/php/upload/postjson.php"];
// 請(qǐng)求
NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:URL];
// 設(shè)置請(qǐng)求方法
requestM.HTTPMethod = @"POST";
// 設(shè)置請(qǐng)求體
requestM.HTTPBody = jsonData;
// 發(fā)送請(qǐng)求
[[[NSURLSession sharedSession] dataTaskWithRequest:requestM completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 處理響應(yīng)
if (error == nil && data != nil) {
// 反序列化
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
} else {
NSLog(@"%@",error);
}
}] resume];
}