iOS:POST,其中JSON只是众多参数中的一个
通过将JSON的数据表示形式设置为HTTP正文,我已成功地将JSON用作我的应用程序中发布webservices的参数:iOS:POST,其中JSON只是众多参数中的一个,ios,json,cocoa,post,Ios,Json,Cocoa,Post,通过将JSON的数据表示形式设置为HTTP正文,我已成功地将JSON用作我的应用程序中发布webservices的参数: //Prep NSData *requestData = [NSJSONSerialization dataWithJSONObject:<my JSON> options:0 error:nil]; NSString *apiString = <the webservice>; //POST NSMutabl
//Prep
NSData *requestData = [NSJSONSerialization dataWithJSONObject:<my JSON> options:0 error:nil];
NSString *apiString = <the webservice>;
//POST
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];
//Go
NSURLResponse *response = nil;
queryResultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
必须作为帖子提交
有趣的是,JSON字典只是许多参数中的一个参数,而不是在主体中。我不知道如何在帖子中添加这些参数
编辑
在php中也有相同的概念。用可可粉怎么做
$data = array('x' => 1, 'name => 'fred');
$json = json_encode($data);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array('json_data' => $json));
-- or --
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'something=whatever&json_data='.$json);
$data = array('x' => 1, 'name => 'fred');
$json = json_encode($data);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array('json_data' => $json));
-- or --
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'something=whatever&json_data='.$json);