$_POST['name']; 所有的第三方交互都是这样的, 包括c◆◆,android 等,我都用过
可以直接使用流上传,不需要进行编码,然后php获取后直接保存就可以了,如:
$byte = file_get_contents('php://input');
file_put_contents($filename,$byte);
导语:JSON作为数据包格式传输的时候具有更高的效率,这是因为JSON不像XML那样需要有严格的闭合标签,这就让有效数据量与总数据包比大大提升,从而减少同等数据流量的情况下,网络的传输压力.JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端程序.这个字符串看起来有点儿古怪,但是JavaScript很容易解释它,而且 JSON 可以表示比"名称 / 值对"更复杂的结构.例如,可以表示数组和复杂的对象,而不仅仅是键和值的简单列表.
怎么生成和解析iOS开发JSON格式数据?
第一段:如何生成JSON格式的'数据?
①.、利用字典NSDictionary转换为键/值格式的数据.
// 如果数组或者字典中存储了 NSString, NSNumber, NSArray, NSDictionary, or NSNull 之外的其他对象,就不能直接保存成文件了.也不能序列化成 JSON 数据.
NSDictionary *dict = @{@"name" : @"me", @"do" : @"something", @"with" : @"her", @"address" : @"home"};
// 1.判断当前对象是否能够转换成JSON数据.
// YES if obj can be converted to JSON data, otherwise NO
BOOL isYes = [NSJSONSerialization isValidJSONObject:dict];
if (isYes) {
NSLog(@"可以转换");
*/
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
/*
Writes the bytes in the receiver to the file specified by a given path.
YES if the operation succeeds, otherwise NO
// 将JSON数据写成文件
// 文件添加后缀名: 告诉别人当前文件的类型.
// 注意: AFN是通过文件类型来确定数据类型的!如果不添加类型,有可能识别不了! 自己最好添加文件类型.
[jsonData writeToFile:@"/Users/SunnyBoy/Sites/JSON_XML/dict.json" atomically:YES];
} else {
NSLog(@"JSON数据生成失败,请检查数据格式");
}
BOOL isYes = [NSJSONSerialization isValidJSONObject:array];
NSData *data = [NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];
[data writeToFile:@"/Users/SunnyBoy/Sites/JSON_XML/base" atomically:YES];
第二段:如何解析JSON格式的数据?
①.、使用TouchJSon解析方法:(需导入包:#import "TouchJson/JSON/CJSONDeserializer.h")
//使用TouchJson来解析北京的天气
//获取API接口
NSURL *url = [NSURL URLWithString:@""];
//定义一个NSError对象,用于捕获错误信息
NSError *error;
NSLog(@"jsonString---%@",jsonString);
//因为返回的Json文件有两层,去第二层内容放到字典中去
NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
NSLog(@"weatherInfo---%@",weatherInfo);
//取值打印
NSLog(@"%@",[NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]);
//使用SBJson解析北京的天气
NSError *error = nil;
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *rootDic = [parser objectWithString:jsonString error:error];
NSLog(@"%@", [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]);
// 从中国天气预报网请求数据
NSURL *url = [ NSURL URLWithString:@""];
// 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 在网络完成的 Block 回调中,要增加错误机制.
// 失败机制处理: 错误的状态码!
// 最简单的错误处理机制:
if (data !error) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSDictionary *dict = obj[@"weatherinfo"];
NSLog(@"%@---%@", dict, dict[@"city"]);
}] resume];
//如果json是"单层"的,即value都是字符串、数字,可以使用objectFromJSONString
NSLog(@"json1:%@",json1);
NSDictionary *data1 = [json1 objectFromJSONString];
NSLog(@"json1.a:%@",[data1 objectForKey:@"a"]);
NSLog(@"json1.b:%@",[data1 objectForKey:@"b"]);
//如果json有嵌套,即value里有array、object,如果再使用objectFromJSONString,程序可能会报错(测试结果表明:使用由网络或得到的php/json_encode生成的json时会报错,但使用NSString定义的json字符串时,解析成功),最好使用objectFromJSONStringWithParseOptions:
土嘎嘎的粉丝们大家好!
传文件不能用POST接收,并且发送文件类型需要在表单里面添加enctype="multipart/form-data"这个才能发送文件.如:form action="{:U('Admin/Task/addhandle')}" method="post" enctype="multipart/form-data"
在控制器中需要用$_FILES这个来接收上传的文件数据.
php
$streamData = file_get_contents('php://input', 'r');//字节流
$fullPath = 'test.png';
$reArr=array();
if(!file_exists($fullPath)){
$reArr['imgPath'] = 'test.png';
$h = fopen($fullPath, 'a◆');
if($h){
if(fwrite($h, $streamData)){
fclose($h);
$reArr['status'] = 1;
}else{
$reArr['status'] = 0;
如果你用php://input能取到值的话,用$_POST应该是能取到值的
不同的是两个取值的结果不一样,php://input取到的是个字符串,$_POST取到的是一个数组
你用print_r($_POST)看一下
以上就是土嘎嘎小编为大家整理的php获取ios数据类型相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!