iOS之JSON序列化(自定义对象,字典,数组的序列化)

注意:

要序列化的对象:

最外层的对象类型必须是NSArray或NSDictionary;

字典的key必须是NSSting类型;

数字不能是NAN或无限大;

所有的对象类型必须时NString,NSNumber,NSAray,NSDIctionary,NSNull;




- (void)viewDidLoad {

    [super viewDidLoad];

    // 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 = [[HMVideo allocinit];

    v1.videoName = @"ll-001.avi";

    v1.size = 500;

    v1.author = @"lilei";

    //KVC给对象内部的成员变量赋值

    [v1 setValue:@(YES) forKey:@"_isYellow"];

    

    HMVideo *v2 = [[HMVideo allocinit];

    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 = [NSMutableArray arrayWithCapacity:2];

    for (HMVideo *video in array) {

        NSDictionary *dic = [video dictionaryWithValuesForKeys:@[@"videoName",@"size",@"author",@"_isYellow"]];

        [mArray addObject:dic];

    }

    

    NSData *data = [NSJSONSerialization dataWithJSONObject:mArray options:0error:NULL];

    [self postJSON:data];

    

    

    

}


//

- (void)postJSON:(NSData *)data {

    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/php/upload/postjson.php"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //设置post

    request.HTTPMethod = @"post";

    request.HTTPBody = data;

    

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueuecompletionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullabledata, NSError * _Nullable connectionError) {

        if (connectionError) {

            NSLog(@"连接错误 %@",connectionError);

            return;

        }

        

        //

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

        if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {

            //解析数据

            NSString *str = [[NSString allocinitWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"%@",str);

        }else{

            NSLog(@"服务器内部错误");

        }

    }];


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值