Php json_解码问题-语法错误,json格式错误
我从php收到一个json数组,作为php中curl_exec的返回(第一个json php->python,返回另一个json),由于语法错误,解码失败 这段API代码:Php json_解码问题-语法错误,json格式错误,php,Php,我从php收到一个json数组,作为php中curl_exec的返回(第一个json php->python,返回另一个json),由于语法错误,解码失败 这段API代码: if($_GET['url'] == 'tomorrowdate'){ $tomorrow = date('Y-m-d', strtotime(' + 1 days')); $risposta = [ "tomorrow" => $tomorrow ]; echo jso
if($_GET['url'] == 'tomorrowdate'){
$tomorrow = date('Y-m-d', strtotime(' + 1 days'));
$risposta = [
"tomorrow" => $tomorrow
];
echo json_encode($risposta);
http_response_code(200);
}
curl代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
//var_dump($output);
$data = stripslashes($data);
$json_array = json_decode($output, true);
//var_dump(curl_error($ch));
curl_close($ch);
var_dump($json_array);
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;
}
我试图用我的代码修改你的代码,但问题仍然存在
function remove_utf8_bom($text){
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
$tomorrow = date('Y-m-d', strtotime(' + 1 days'));
$risposta = [
"tomorrow" => $tomorrow
];
$json = remove_utf8_bom($risposta);
echo json_encode($json);
var_dump(json_decode($json_encode, TRUE));
输出为:
{"tomorrow":"2018-09-15"}NULL - Syntax error, malformed JSON
使用以下代码,我可以看到JSON开头有一个不可打印的字符:
$json = '{"tomorrow":"2018-09-15"}';
var_dump(json_encode($json));
返回:
string(37) ""\ufeff{\"tomorrow\":\"2018-09-15\"}""
字符串ufeff
是一个字符串。要删除它,请使用以下功能:
function remove_utf8_bom($text){
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
返回:
string(31) ""{\"tomorrow\":\"2018-09-15\"}""
string(31) ""{\"tomorrow\":\"2018-09-15\"}""
Array
(
[tomorrow] => 2018-09-15
)
现在使用所有代码:
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
$json = remove_utf8_bom('{"tomorrow":"2018-09-15"}');
var_dump(json_encode($json));
print_r(json_decode($json, TRUE));
返回:
string(31) ""{\"tomorrow\":\"2018-09-15\"}""
string(31) ""{\"tomorrow\":\"2018-09-15\"}""
Array
(
[tomorrow] => 2018-09-15
)
根据评论进行编辑:
更改代码的最后几行:
$json = remove_utf8_bom(json_encode($risposta)); // encode here
//echo json_encode($json); // don't really need this, just a test
var_dump(json_decode($json, TRUE)); // you had $json_encode here
这将返回:
在我的例子中,我之所以出现这个错误是因为PHP版本 如果您的php版本小于或等于5.4.0,则必须以引号发送所有json键值 我在发送有效载荷
$payload = array(
"id" => 36
);
在JWTs解码函数中解码json时失败
我换了这个
$payload = array(
"id" => "36"
);
成功了
这是因为在以前的PHP版本中,json_解码要求在引号中给出值。
这是JWT.php JWT库中的代码块
public static function jsonDecode($input)
{
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
/** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
* to specify that large ints (like Steam Transaction IDs) should be treated as
* strings, rather than the PHP default behaviour of converting them to floats.
*/
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
} else {
/** Not all servers will support that, however, so for older versions we must
* manually detect large ints in the JSON string and quote them (thus converting
*them to strings) before decoding, hence the preg_replace() call.
*/
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
$obj = json_decode($json_without_bigints);
}
if (function_exists('json_last_error') && $errno = json_last_error()) {
static::handleJsonError($errno);
} elseif ($obj === null && $input !== 'null') {
throw new DomainException('Null result with non-null input');
}
return $obj;
}
enter code here
请将JSON复制并粘贴到原始帖子的编辑中。如果答案解决了问题,请考虑接受答案。下面是如何返回此处并对勾号/复选标记执行相同操作,直到其变为绿色。这将通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,并可能希望发布(更多)答案。您将获得积分,并鼓励其他人帮助您。欢迎来到Stack!编辑我的回答我试图用我的代码修改你的代码,但问题仍然存在。。。函数remove_utf8_bom($text){$bom=pack('H*','EFBBBF');$text=preg_replace(“/^$bom/”,'','','$text);返回$text;}$tomory=date('Y-m-d','strotime('+1天')$Rispesta=[“明天”=>明天美元]$json=删除物料清单($RISPASTA);echo json_encode($json);var_dump(json_decode($json_encode,TRUE));输出为:{“明天”:“2018-09-15”}NULL-语法错误,格式错误JSON请不要在注释中转储代码。编辑您的原始帖子,以包含任何新代码或更改。如果无法访问您的服务器,我无法猜测真正的问题。代码返回一个有效数组。