JSON序列化

来源:互联网 发布:京东618和双11数据 编辑:程序博客网 时间:2024/06/07 16:00

//

//  ViewController.m

//  09-JSON序列化

//

//  Created by Apple on 15/10/23.

//  Copyright © 2015 heima. All rights reserved.

//


#import "ViewController.h"

#import "HMVideo.h"

@interface ViewController ()


@end


@implementation ViewController


- (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 = [[HMVideo alloc] init];

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

    v1.size = 500;

    v1.author = @"lilei";

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

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

    

    HMVideo *v2 = [[HMVideo alloc] 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 = [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:0 error: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:[NSOperationQueue mainQueue] 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 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"%@",str);

        }else{

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

        }

    }];


}


@end


0 0
原创粉丝点击