检测PHP json_decode()中的不良json数据?

我想通过json_decode()parsing时处理错误的json数据。 我正在使用以下脚本:

if(!json_decode($_POST)) { echo "bad json data!"; exit; } 

如果$ _POST等于:

 '{ bar: "baz" }' 

然后json_decode处理错误罚款,并吐出“坏的JSON数据!”; 但是,如果我将$ _POST设置为“无效数据”,它会给我:

 Warning: json_decode() expects parameter 1 to be string, array given in C:\server\www\myserver.dev\public_html\rivrUI\public_home\index.php on line 6 bad json data! 

我是否需要编写一个自定义脚本来检测有效的json数据,还是有一些其他漂亮的方法来检测?

这里有几个关于json_decode

  • 它返回数据,或者在出现错误时返回null
  • 当没有错误时它也可以返回null :当JSONstring包含null
  • 在有警告的地方会发出警告 – 警告您要消失。

为了解决警告问题,一个解决scheme是使用@操作符 (我不经常推荐使用它,因为它使debugging变得更加困难…但是在这里,没有太多的select)

 $_POST = array( 'bad data' ); $data = @json_decode($_POST); 

然后你必须testing$data是否为null – 为了避免json_decode在JSONstring中返回null的情况,你可以检查json_last_error ,哪个(引用)

返回上次JSONparsing发生的最后一个错误(如果有的话)。

这意味着你必须使用如下代码:

 if ($data === null && json_last_error() !== JSON_ERROR_NONE) { echo "incorrect data"; } 

你也可以使用json_last_error: http ://php.net/manual/en/function.json-last-error.php

作为文档说:

返回上次JSON编码/解码期间发生的最后一个错误(如果有的话)。

这里是一个例子

 json_decode($string); 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; } 

我只是从一个url编码的string打破了我的头在json语法错误中看起来是完美的json:{“test1”:“car”,“test2”:“auto”}。

但在我的情况下,上面的一些是HTML编码,因为添加html_entity_decode($string)的窍门。

 $ft = json_decode(html_entity_decode(urldecode(filter_input(INPUT_GET, 'ft', FILTER_SANITIZE_STRING)))); 

希望这会节省一些别人的时间。

/ ** * *自定义json_decode *处理json_decode错误* * @参数types$ json_text * @returntypes* /公共静态函数custom_json_decode($ json_text){

  $decoded_array = json_decode($json_text, TRUE); switch (json_last_error()) { case JSON_ERROR_NONE: return array( "status" => 0, "value" => $decoded_array ); case JSON_ERROR_DEPTH: return array( "status" => 1, "value" => 'Maximum stack depth exceeded' ); case JSON_ERROR_STATE_MISMATCH: return array( "status" => 1, "value" => 'Underflow or the modes mismatch' ); case JSON_ERROR_CTRL_CHAR: return array( "status" => 1, "value" => 'Unexpected control character found' ); case JSON_ERROR_SYNTAX: return array( "status" => 1, "value" => 'Syntax error, malformed JSON' ); case JSON_ERROR_UTF8: return array( "status" => 1, "value" => 'Malformed UTF-8 characters, possibly incorrectly encoded' ); default: return array( "status" => 1, "value" => 'Unknown error' ); } } 

这是怎样处理JSON

  /** * Parse the JSON response body and return an array * * @return array|string|int|bool|float * @throws RuntimeException if the response body is not in JSON format */ public function json() { $data = json_decode((string) $this->body, true); if (JSON_ERROR_NONE !== json_last_error()) { throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error()); } return $data === null ? array() : $data; }