美文网首页
post发送JSON数据(字符串、数组、字典、自定义对象)给服务

post发送JSON数据(字符串、数组、字典、自定义对象)给服务

作者: reloadRen | 来源:发表于2016-06-04 16:33 被阅读1168次

post发送JSON数据给服务器

触发发送的方法
  • 这次Demo是通过点击屏幕触发发送数据给服务器事件
  • 前提需要开启本地模拟服务器
-  (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post/postjson.php"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
    
    request.HTTPMethod = @"POST";
    
    //选择不同类型的data发送给服务器
//    request.HTTPBody = [self jsonString];

//    request.HTTPBody = [self dictionaryData];

//    request.HTTPBody = [self arrayData];
    
    request.HTTPBody = [self objectData];
    
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        if (data == nil || connectionError != nil) {
            NSLog(@"请求数据失败!");
            return ;
        }
        
//        id recive = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//服务器返回的时字符串
        NSString *recive = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@", recive);
     
    }];
#pragma clang diagnostic pop
}
发送JSON字符串
- (NSData *)jsonString{

NSString *string = @"{\"name\":\"zhangsan\",\"age\":\"18\"}";

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

return data;
}
发送字典给服务器
- (NSData *)dictionaryData{

NSDictionary *dict = @{
                       @"name":@"zhangsan",
                       @"age":@18
                       
                       };
//通过序列化成data类型
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];

return data;
}
发送数组给服务器
- (NSData *)arrayData{

NSArray *array = @[
                   @{@"zhangsan":@18},
                   @{@"lisi":@20}
                   ];
NSData *data =[NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];

return data;
}
发送oc对象给服务器
  • 先将对象转换为字典
  • 通过系统提供的JSON解析类进行序列化
    - (NSData *)objectData{

    RZPerson *person = [[RZPerson alloc]init];
    
    person.name = @"zhangsan";
    
    person.age = 20;
    
    //先将对象转换为字典类型
    NSDictionary *dict = [person dictionaryWithValuesForKeys:@[@"name",@"age"]];
    
    //将字典转换为data类型
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];

    return data;
    }

相关文章

  • post发送JSON数据(字符串、数组、字典、自定义对象)给服务

    post发送JSON数据(字符串、数组、字典、自定义对象)给服务器 触发发送的方法 这次Demo是通过点击屏幕触发...

  • post发送JSON数据(字符串、数组、字典、自定义对象)给服务

    post发送JSON数据给服务器 触发发送的方法 这次Demo是通过点击屏幕触发发送数据给服务器事件 前提需要开启...

  • JSON序列化

    POST_JSON序列化:将字典或者数组 转换成二进制数据 准备发送给服务器。反序列化:从服务器接收到 二进制数据...

  • php

    /** 字符串 **/ json串转数组,每个元素为对象 $itemes = json_decode($_POST...

  • 5.1 json

    1 .发送网络请求,请求数据,数据返回,直接设置数据 json :是字典和数组的统称.也可以说是字典和数组的组合吧...

  • python json 模块

    json转换 json.dumps() 将字典对象转换为字符串 json.loads() 将字符串对象转换为字典 ...

  • JSON库

    JSON库将json对象或节点转换成字典、将json数组转换成列表、将json字符串转换成python字符串,tr...

  • OC_YYModel字典转模型的几种详细用法

    目录 JSON转字符串 普通字典转模型 模型属性有自定义的模型YYUSer 属性有数组(数组里自定义模型),还有字...

  • iOS归档看这篇就够了

    归档的作用 之前将数据存储到本地,只能是字符串、数组、字典、NSNuber、BOOL等容器类对象,不能将自定义对象...

  • 爬虫学习(三)json字符串与数据库

    1.1 json字符串支持的数据格式 1.对象(字典)用 {} 表示2.数组(列表)用 [] 表示3.整形,浮点型...

网友评论

      本文标题:post发送JSON数据(字符串、数组、字典、自定义对象)给服务

      本文链接:https://www.haomeiwen.com/subject/ofbldttx.html