POST 上传 JSON 数据
//
// ViewController.m
// 03-post上传json
//
// Created by jerry on 15/10/10.
// Copyright (c) 2015年 jerry. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1. url
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/post/postjson.php"]; // 2. post Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: timeoutInterval:2.0f];
// 2.1 set HTTPMethod
request.HTTPMethod = @"POST"; // 2.2 UpLoad JSON Type Data
// 序列化,将NSArray/NSDictionary转换成特殊数据类型的二进制数据。
// 反序列化 将服务器返回的二进制数据转成NSArray/NSDictionary
NSDictionary *dict1 = @{@"name":@"xiaofeng",@"age":@""};
NSDictionary *dict2 = @{@"name":@"xiaosan",@"age":@""}; NSArray *array = @[dict1,dict2];
/**
- Top level object is an NSArray or NSDictionary
顶级节点是字典或者数组
- All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
所有的对象是 NSString, NSNumber, NSArray, NSDictionary, or NSNull
- All dictionary keys are NSStrings
所有字典的key 是 NSString
- NSNumbers are not NaN or infinity
NSNumbers必须指定,不能是无穷大 + (BOOL)isValidJSONObject:(id)obj;
用来检验给定的对象是否能够被序列化
*/
// NSString *str = @"{\"username\":\"xiangfeng\",\"age\":\"18\"}";
if (![NSJSONSerialization isValidJSONObject:array]) {
NSLog(@"格式不正确,不能被序列化!");
return ;
}
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:array options: error:NULL]; // connection
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 反序列化的一个处理
id result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
POST 上传 JSON 数据的更多相关文章
- [iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数
A.上传JSON 1.思路: 必须使用POST方法才能上传大量JSON数据 设置请求头:设置Content-Type 设置请求体,JSON实际相当于字典,可以用NSDictionary NSJSONS ...
- springmvc04-文件上传-JSON数据
文件上传部分: 1, 导入commons-fileupload-1.2.2.jar commons-io-2.4.jar 两个jar包. 2, 在主配置文件中,添加如下信息 <!-- 文件上传- ...
- php curl 上传json数据
PUT $data = array('username'=>'dog','password'=>'tall'); $data_json = json_encode($data); $ch ...
- C语言采用socket实现http post方式上传json数据
1.按照HTTP协议发送请求: http POST 报文格式 http 报文是面向文本的. 报文分为:请求报文和响应报文 请求报文由:请求行,请求头部,空行和请求数据四个部分组成. <1.请求行 ...
- HttpUrlConneciton上传JSON数据
try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConn ...
- xUtils怎么post请求上传json数据
InfoSmallCodeBinding smallCode = new InfoSmallCodeBinding(); smallCode.setSmallCode("测试"); ...
- (24)ajax上传json格式的数据
urs.py from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpat ...
- Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串
文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...
- Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单
如果嫌麻烦直接可以用我封装好的库:Novate: https://github.com/Tamicer/Novate 通过对Retrofit2.0的前两篇的基础入门和案例实践,掌握了怎么样使用Retr ...
随机推荐
- BZOJ5372 PKUSC2018神仙的游戏(NTT)
首先有一个想法,翻转串后直接卷积看有没有0匹配上1.但这是必要而不充分的因为在原串和翻转串中?不能同时取两个值. 先有一些结论: 如果s中长度为len的前缀是border,那么其存在|s|-len的循 ...
- BZOJ1093 ZJOI2007最大半连通子图(缩点+dp)
发现所谓半连通子图就是缩点后的一条链之后就是个模板题了.注意缩点后的重边.写了1h+真是没什么救了. #include<iostream> #include<cstdio> # ...
- Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)
题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...
- 自学Zabbix7.1 IT services
自学Zabbix7.1 IT services 1. 概念IT Services 服务器或者某项服务.业务的可用率,不懂技术的上级领导会过问最近服务器可用率如何.所有api的状况怎么样?通常一些技术人 ...
- sql server 小技巧(2) 删除sql server中重复的数据
with list_numbers as ( select Name, AuthorOrTime, Url, Price, EstimatePrice, Size, Category, ROW_NUM ...
- Linux中无权限使用sudo
提示:$USER is not in the sudoers file. This incident will be reported. 解决方法: 切换到root用户,su 编辑sudo配置文件,v ...
- 2018 省选 D1T2 IIIDX
题目大意: 给出k.n个数选择一种字典序最大的排列,使得对于任意的i,di>=d[i/k](下取整 下同) 分析: 很容易想到的是建树,将i的父亲设为[i/k],之后建有向边. 60分贪心: 将 ...
- A1089. Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- max,min,Zip函数(十一)
zip函数,拉链,传两个有序的参数,将他们一一对应为元祖形式 max,min比较默认比较一个元素,处理的是可迭代对象,相当于for循环取出每个元素进行比较,注意:不同类型之间不可比较 #!/usr/b ...
- 搭建一个简单的node.js服务器
第一步:安装node.js.可以去官网:https://nodejs.org/en/进行下载. 查看是否成功,只需在控制台输入 node -v.出现版本号的话,就证明成功了. 第二步:编写node.j ...