PHP:json_decode 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24001410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: json_decode not working
提问by yoshi
This does notwork:
这并不能正常工作:
$jsonDecode = json_decode($jsonData, TRUE);
However if I copy the string from $jsonData
and put it inside the decode function manually it does work.
但是,如果我从中复制字符串$jsonData
并将其手动放入 decode 函数中,它确实可以工作。
This works:
这有效:
$jsonDecode = json_decode('{"id":"0","bid":"918","url":"http:\/\/www.google.com","md5":"6361fbfbee69f444c394f3d2fa062f79","time":"2014-06-02 14:20:21"}', TRUE);
I did output $jsonData
copied it and put in like above in the decode function. Then it worked. However if I put $jsonData
directly in the decode function it does not.
我确实输出$jsonData
复制了它并像上面一样放入解码函数中。然后它起作用了。但是,如果我$jsonData
直接放入 decode 函数中,则不会。
var_dump($jsonData)
shows:
var_dump($jsonData)
显示:
string(144) "{"id":"0","bid":"918","url":"http:\/\/www.google.com","md5":"6361fbfbee69f444c394f3d2fa062f79","time":"2014-06-02 14:20:21"}"
The $jsonData
comes from a encrypted $_GET
variable. To encrypt it I use this:
在$jsonData
来自一个加密$_GET
变量。为了加密它,我使用这个:
$key = "SOME KEY";
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_ECB, $iv);
$iv = rawurlencode(base64_encode($iv));
$enc = rawurlencode(base64_encode($enc));
//To Decrypt
$iv = base64_decode(rawurldecode($_GET['i']));
$enc = base64_decode(rawurldecode($_GET['e']));
$data = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $enc, MCRYPT_MODE_ECB, $iv);
采纳答案by laurent
Most likely you need to strip off the padding from your decrypted data. There are 124 visible characters in your string but var_dump
reports 144. Which means 20 characters of padding needs to be removed (a series of "\0" bytes at the end of your string).
您很可能需要从解密数据中去除填充。您的字符串中有 124 个可见字符,但var_dump
报告了 144 个。这意味着需要删除 20 个填充字符(字符串末尾的一系列“\0”字节)。
Probably that's 4 "\0" bytes at the end of a block + an empty 16-bytes block (to mark the end of the data).
可能是块末尾的 4 个“\0”字节 + 一个空的 16 字节块(用于标记数据的结尾)。
How are you currently decrypting/encrypting your string?
您目前如何解密/加密您的字符串?
Edit:
编辑:
You need to add this to trim the zero bytes at the end of the string:
您需要添加它以修剪字符串末尾的零字节:
$jsonData = rtrim($jsonData, "$jsonData = stripslashes(html_entity_decode($jsonData));
$k=json_decode($jsonData,true);
print_r($k);
");
回答by user2987827
some time there is issue of html entities, for example \"it will represent like this \", so you must need to parse the html entites to real text, that you can do using html_entity_decode()method of php.
有时会出现 html 实体的问题,例如\"它将表示为这样的\",因此您必须将html 实体解析为真实文本,您可以使用 php 的html_entity_decode()方法来完成。
$jsonDecode = json_decode(trim($jsonData), TRUE);
回答by dar7yl
Judging from the other comments, you could use,
从其他评论来看,你可以使用,
//Remove UTF8 Bom
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
回答by Akhilesh Kumar
While moving on php 7.1 I encountered with json_decode error number 4 (json syntex error). None of the above solution on this page worked for me.
在继续 php 7.1 时,我遇到了 json_decode 错误号 4(json 语法错误)。此页面上的上述解决方案均不适合我。
After doing some more searching i found solution at https://stackoverflow.com/a/15423899/1545384and its working for me.
在做了更多搜索之后,我在https://stackoverflow.com/a/15423899/1545384找到了解决方案,它对我有用。
header('Content-type: application/json;');
回答by amrography
Be sure to set header to JSON
确保将标头设置为 JSON
<?php
$sql = <<<EOT
SELECT *
FROM `students`;
EOT;
$string = '{ "query" : "' . str_replace("\t", " ", str_replace("\n", " ", $sql)).'" }';
print_r(json_decode($string));
?>
回答by profimedica
str_replace("\t", " ", str_replace("\n", " ", $string))
str_replace( "\t", " ", str_replace( "\n", " ", $string))
because json_decode does not work with special characters. And no error will be displayed. Make sure you remove tab spaces and new lines. Depending on the source you get your data, you might need also: stripslashes(html_entity_decode($string))
因为 json_decode 不适用于特殊字符。并且不会显示错误。确保删除制表符空格和新行。根据您获取数据的来源,您可能还需要: stripslashes(html_entity_decode($string))
Works for me:
对我有用:
stdClass Object
(
[query] => SELECT * FROM `students`;
)
output:
输出:
$json_string = stripslashes(html_entity_decode($json_string));
回答by Mindaugas Dobilas
I had problem that json_decode
did not work, solution was to change string encoding to utf-8. This is important in case you have non-latin characters.
我遇到了json_decode
不起作用的问题,解决方案是将字符串编码更改为 utf-8。如果您有非拉丁字符,这很重要。
回答by Hassan Qasim
You have to use preg_replace for avoiding the null results from json_decode
您必须使用 preg_replace 来避免 json_decode 的空结果
here is the example code
这是示例代码
$bookingdata = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
preg_replace( "/\p{Cc}*$/u", "", $data)
回答by Diwakar Padmaraja
Interestingly mcrypt_decrypt seem to add control characters other than \0 at the end of the resulting text because of its padding algorithm. Therefore instead of rtrim($jsonData, "\0")
it is recommended to use
有趣的是 mcrypt_decrypt 似乎在结果文本的末尾添加了 \0 以外的控制字符,因为它的填充算法。因此,而不是rtrim($jsonData, "\0")
建议使用
<?php
$json = preg_replace('/[[:cntrl:]]/', '', $json_data);
$json_array = json_decode($json, true);
echo json_last_error();
echo json_last_error_msg();
print_r($json_array);
?>
on the result $data of mcrypt_decrypt. json_decode will work if all trailing control characters are removed. Pl refer to the comment by Peter Bailey at http://php.net/manual/en/function.mdecrypt-generic.php.
在 mcrypt_decrypt 的结果 $data 上。如果删除所有尾随控制字符, json_decode 将起作用。请参考 Peter Bailey 在http://php.net/manual/en/function.mdecrypt-generic.php的评论。
回答by Anil Rawat
USE THIS CODE
使用此代码
##代码##