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

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 ]]] )

图片转代码服务由CSDN问答提供 功能建议

因为我更喜欢更面向对象的方法,所以我编写了自己的json类来处理json_last_error以进行解码和编码。< / p>

不知怎的,我得到了一个关于json_decode方法的深度属性的php警告。

json_decode方法的PHP核心api(eclipse PDT)看起来像 以下内容:

  function json_decode($ json,$ assoc = null,$ depth = null){} 
   
 
 <  p>到目前为止一切都那么好但是如果我自己写这样的类: 
 
 
  function my_json_decode($ json,$ assoc = null,$ depth = null){
 return  json_decode($ json,$ assoc,$ depth); 
} 
   
 
 

并尝试按以下方式运行它:

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

我得到以下信息 警告:

 警告:json_decode():深度必须大于零/ / n   
 
 

我是否错过了 什么? 我想如果我将属性的null传递给一个将属性本身设置为null的方法,它应该没问题?!

使用:服务器:Apache / 2.2.22(Unix)PHP / 5.3.10

感谢您的帮助!


[编辑]以澄清我的理解泄漏的位置:< / p>

我正在使用Eclipse Indigo + PDT。 org.eclipse.php.core.language的PDT PHP核心api与php.net关于json_decode的说法不同:

json_decode org.eclipse.php.core.language:

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

json_decode php.net:< / p>

  json_decode(string $ json [,bool $ assoc = false [,int $ depth = 512 [,int $ options = 0]]])
  <  / pre> 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题