在PHP json_decode()中检测错误的json数据?

问题描述:

我想通过json_decode()解析时处理坏的json数据。我使用下面的脚本:在PHP json_decode()中检测错误的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数据,或者是有检测其他一些巧妙的方法这个?

+0

'$ _POST'总是一个数组c保留通过POST传递的* x-www-form-urlencoded *参数。你如何将你的数据发送到你的PHP脚本? – Gumbo 2010-02-27 16:58:45

+0

PHP中包含的json函数没有多大帮助。他们有很多问题。看看[json.org](http://json.org/)找到一个好的图书馆。 – whiskeysierra 2010-02-27 17:02:00

这里有一个关于json_decode几件事情:

  • 它返回的数据,或者null时出现错误
  • 它也可以返回null时没有错误:当JSON字符串包含null
  • 它会在出现警告时发出警告 - 警告您要消失。


为了解决这个问题的警告,一个解决方案是使用@ operator(我不经常建议使用它,因为它使调试运行了很多困难......但在这里,没有多少选择的余地)

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

你会再要测试是否$datanull - 和,避免了情况,其中json_decode回报nullnull在JSON字符串,可以ç赫克json_last_error,这(引用)

返回的最后一个错误(如果有的话)发生在去年JSON解析 。


这意味着你必须用一些类似于下面的代码:

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期间发生的最后一个错误(如果有的话) 编码/解码。

这里有一个例子

+6

在http://php.net/manual/en/function.json-last-error-msg.php中,这在PHP 5.5中是完全不必要的 – vvondra 2015-03-04 00:16:00

我刚刚打破了我的头以上的似乎是完美的JSON一个JSON语法错误:{“测试1”:“汽车”,“测试2”:“汽车“}从一个url编码的字符串。

但在我的情况下,以上的一些是HTML编码,因为添加html_entity_decode($string)做了伎俩。

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

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

/** * * 定制json_decode *手柄json_decode错误 * * @参数类型$ json_text * @返回类型 */ 公共静态功能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; 
}