>我使用 Volatility 将 JSON 对象发送到服务器并在服务器中获取数据,但我无法在 PHP 中将 Jason 对象转换为数组,我的代码剂量不起作用
{
"type": "get_new_products",
"city": "abhar",
"page": 0
}
PHP代码
<?php
$get_post = file_get_contents('php://input');
$post_data = json_decode($get_post, true);
$content_type = $post_data['type'];
echo $content_type; ?>
这可能是由于编码。尝试使用 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 手册中复制。
资源
- utf8_decode - 手动
- json_last_error(( - 手动