我发送json对象到服务器与volley并在服务器中获取数据,但我不能将jason对象转换为php中的数组,我的代码不起作用
{
"type": "get_new_products",
"city": "abhar",
"page": 0
}
php code
<?php
$get_post = file_get_contents('php://input');
$post_data = json_decode($get_post, true);
$content_type = $post_data['type'];
echo $content_type; ?>
答案 0 :(得分:1)
可能是由于编码。尝试使用utf8_decode()
。
$jsonString = '{"type": "get_new_products","city": "abhar","page": 0}';
$decodedJson = utf8_decode($jsonString);
// Second parameter must be true to output an array
$jsonArray = json_decode($decodedJson, true);
// Error handling
if (json_last_error()) {
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo 'No errors';
break;
case JSON_ERROR_DEPTH:
echo 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo 'Unknown error';
break;
}
}
// Output values
echo "The type is: ".$jsonArray['type']."\n";
echo "The city is: ".$jsonArray['city']."\n";
The type is: get_new_products
The city is: abhar
The page is: 0
echo "The page is: ".$jsonArray['page']."\n";
错误处理已从PHP.net手册中复制。