[英]json_decode return null when data pass through ios
this problem is very tricky. 这个问题非常棘手。 when I pass json data through IOS by using AFNetworking. 当我通过使用AFNetworking通过IOS传递json数据时。 the server also return null value. 服务器还返回空值。 However, I used curl to test server side, the result is correct. 但是,我使用curl来测试服务器端,结果是正确的。 I have no idea about this problem. 我不知道这个问题。
Here is server code: 这是服务器代码:
$response['return'] = $data;
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
$response['sssss'] = $data;
$data = json_decode($data, TRUE);
$response['return json'] = $data ? $data : 'dddddddd';
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
$response['json error'] = $json_errors[json_last_error()];
$response['p'] = $data->name;
$response['d'] = 'test';
ios code : ios代码:
NSURL *url = [NSURL URLWithString:@"myapi"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:dishParameter,@"dish", nil];
[httpClient postPath:@"dish" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
for (id key in responseObject) {
NSLog(@"key:%@ value:%@", key, [responseObject objectForKey:key]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@",error);
}];
the response said that no syntax error and server can get the value of dish parameter. 响应说没有语法错误,服务器可以获取盘参数的值。 but when $data through stripslashes function, $data becomes to null. 但是当$ data通过stripslashes函数时,$ data变为null。
Anyone can give me some suggestion? 有人可以给我一些建议吗?
$data
is type string ? $data
是字符串类型吗? Why would you strip slashes from JSON ? 为什么要从JSON中删除斜杠? You screw the syntax. 您拧紧语法。 Do not do that. 不要那样做。 json_decode
returns null becauuse it's an invalid JSON format. json_decode
返回null,因为它是无效的JSON格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.