json_decode 从 iOS 应用程序返回空值
Posted
技术标签:
【中文标题】json_decode 从 iOS 应用程序返回空值【英文标题】:json_decode returns null value from iOS application 【发布时间】:2012-12-19 01:16:09 【问题描述】:我在解析发送到 php 网络服务的数据时遇到问题。我正在使用以下代码来获取 json:
$decoded = json_decode(file_get_contents('php://input'));
if(is_null($decoded) == NULL)
$body = "Data was not successfully received";
$body = $body . " " . $jsonInput;
json 是从 ios 应用程序发送的,如下所示:
"water" : "YES",
"int_clean" : "YES",
"ceiling_stains" : "YES",
"additional_comments" : "not entered",
"roof_cond" : "YES",
"e_clean" : "YES",
"interior_cond" : "not entered",
"no_exp" : "not entered",
"addr" : "YES",
"roof_leak" : "YES",
"doors_sec" : "YES",
"elec" : "YES",
"ceiling_exp" : "not entered",
"repaired" : "YES",
"o_desc" : "not entered",
"w_sign" : "YES",
"o_cond" : "not entered",
"int_cond" : "YES",
"gas" : "YES",
"for_sale_sign" : "YES",
"sold_as_is" : "YES",
"mb_sign" : "YES",
"graffiti" : "YES",
"date" : "18-12-2012 18:58",
"dewinterized" : "YES",
"HVAC" : "YES",
"why_no_mat" : "not entered",
"is_lockbox" : "YES",
"financing_mat" : "YES",
"yard_cond" : "YES",
"marketing_mat" : "YES",
"HVAC_missing" : "not entered",
"agent_info" : "YES",
"e_cond" : "YES",
"e_key" : "YES",
"pool_sec" : "YES",
"pool_clean" : "YES"
并使用此代码发送:
NSDictionary * info = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://uofi-bars.com/sendEmail.php"]];
[req addValue:@"Content-Type: application/json" forHTTPHeaderField: @"Content-Type"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:jsonData];
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
我对 php 完全陌生,这让我头疼了很长一段时间。任何帮助将不胜感激!
【问题讨论】:
if(is_null($decoded) == NULL)
?为什么不if(is_null($decoded))
【参考方案1】:
这是不正确的if(is_null($decoded) == NULL)
。 PHP is_null
返回一个 boolean
值。所以你需要使用if(is_null($decoded) === FALSE)
或if(is_null($decoded) === TRUE)
。
【讨论】:
以上是关于json_decode 从 iOS 应用程序返回空值的主要内容,如果未能解决你的问题,请参考以下文章
为啥在此 JSON 字符串上使用 json_decode 会导致空值
Angular JS $http.get 从 WP REST API 返回空数据,但仅在实际 iOS 设备上