dqkx69935 2012-07-21 07:43 采纳率: 100%
浏览 456
已采纳

json_decode自己的类 - 警告:深度必须大于零

Because I prefer a more object oriented approach I wrote my own json class to handle also json_last_error for decode and encode.

Somehow I get a php warning for the depth property of the json_decode method.

The PHP core api (eclipse PDT) for the json_decode method looks like the following:

function json_decode ($json, $assoc = null, $depth = null) {}

So far so good but if I write my own class like this:

function my_json_decode ($json, $assoc = null, $depth = null) {
    return json_decode($json, $assoc, $depth);
}

and try to run it as follow:

$json = '{ "sample" : "json" }';
var_dump( my_json_decode($json) );

I get the following warning:

Warning: json_decode(): Depth must be greater than zero in /

Did I miss anything? I thought if I pass null for a property to a method which set the property itself to null it should be fine?!

Using: Server: Apache/2.2.22 (Unix) PHP/5.3.10

Thanks for the help!


[Edit] to clarify where my leak of understanding was:

I am using Eclipse Indigo + PDT. The PDT PHP core api of org.eclipse.php.core.language is different from what php.net say about json_decode:

json_decode org.eclipse.php.core.language:

json_decode ($json, $assoc = null, $depth = null)

json_decode php.net:

json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
  • 写回答

4条回答 默认 最新

  • doucuo1642 2012-07-21 07:53
    关注

    Depth is the recursion depth (should be an INTEGER) of json_decode. See the manual for more details.

    What you are doing is that you are setting $depth to 0. Since your json object's depth is 2. the minimum value of $depth must be 2. Also your code would work fine with any value of depth>2, but I would recommend to use the default value of 512 (it was 128 initially which was later increased to 512 in PHP 5.3.0)

    Also do note that assoc has to be a bool value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?