iOS 之JSON序列化的注意(陣列,字典,自定義物件的序列化)
注意:
要序列化的物件:
最外層的物件型別必須是NSArray或NSDictionary;
字典的key必須是NSSting型別;
數字不能是NAN或無限大;
所有的物件型別必須時NString,NSNumber,NSAray,NSDIctionary,NSNull;
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1 自己拼json形式的字串,比較困難
// NSString *jsonStr = @"{\"name\":\"zhangsan\",\"age\":18}";
// [self postJSON:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
// 2 字典
// NSDictionary *dic = @{@"name":@"zhangsan",@"age":@(18)};
// //JSON序列化
// NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
// [self postJSON:data];
// 3 陣列
// NSArray *array = @[
// @{@"name":@"zhangsan",@"age":@(18)},
// @{@"name":@"lisi",@"age":@(19)}
// ];
// //JSON序列化
// NSData *data = [NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];
// [self postJSON:data];
//4 自定義物件進行JSON序列化
// HMVideo *v1 = [[HMVideo alloc] init];
// v1.videoName = @"ll-001.avi";
// v1.size = 500;
// v1.author = @"lilei";
// //KVC給物件內部的成員變數賦值
// [v1 setValue:@(NO) forKey:@"_isYellow"];
//
//
//// NSLog(@"%@",v1);
// //自定義物件不能進行JSON序列化,必須先把自定義物件轉換成字典或陣列
//// if (![NSJSONSerialization isValidJSONObject:v1])
//// NSLog(@"sorry,物件不能進行json序列化");
//// return;
//// }
//
// //把自定義物件轉換成字典 KVC
// NSDictionary *dic = [v1 dictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];
//
// NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
// [self postJSON:data];
//5 把自定義物件的陣列進行JSON序列化
HMVideo *v1 = [[HMVideoalloc]init];
v1.videoName =@"ll-001.avi";
v1.size =500;
v1.author =@"lilei";
//KVC給物件內部的成員變數賦值
[v1 setValue:@(YES)forKey:@"_isYellow"];
HMVideo *v2 = [[HMVideoalloc]init];
v2.videoName =@"hmm-001.avi";
v2.size =500;
v2.author =@"韓梅梅";
//KVC給物件內部的成員變數賦值
[v2 setValue:@(NO)forKey:@"_isYellow"];
NSArray *array =@[v1,v2];
// if (![NSJSONSerialization isValidJSONObject:array]) {
// NSLog(@"sorry,不能進行JSON序列化");
// return;
// }
//把自定義物件的陣列,所有的物件都轉換成字典
NSMutableArray *mArray = [NSMutableArrayarrayWithCapacity:2];
for (HMVideo *videoin array) {
NSDictionary *dic = [videodictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];
[mArray addObject:dic];
}
NSData *data = [NSJSONSerializationdataWithJSONObject:mArrayoptions:0error:NULL];
[selfpostJSON:data];
}
//
- (void)postJSON:(NSData *)data {
NSURL *url = [NSURLURLWithString:@"http://127.0.0.1/php/upload/postjson.php"];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//設定post
request.HTTPMethod =@"post";
request.HTTPBody = data;
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse *_Nullable response,NSData * _Nullable data,NSError *_Nullable connectionError) {
if (connectionError) {
NSLog(@"連線錯誤 %@",connectionError);
return;
}
//
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode ==200 || httpResponse.statusCode ==304) {
//解析資料
NSString *str = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}else{
NSLog(@"伺服器內部錯誤");
}
}];
}