2012-07-21 121 views
1

因为我更喜欢更面向对象的方法,所以我编写了自己的json类来处理解码和编码的json_last_error。json_decode自己的类 - 警告:深度必须大于零

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

PHP的核心API(日食PDT)的json_decode方法如下所示:

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

到目前为止好,但如果我写我自己的类这样的:

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

和尝试运行它如下:

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

我得到以下警告:

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

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

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

感谢您的帮助!


[编辑]澄清在我的理解是泄漏:

我使用Eclipse靛蓝+ 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:

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

回答

1

深度是json_decode的递归深度(应该是INTEGER)。有关更多详细信息,请参阅manual

你正在做的是你将$ depth设置为0.由于你的json对象的深度是2,所以$ depth的最小值必须是2。此外,您的代码可以在深度> 2的任何值时正常工作,但我会建议使用默认值512(最初为128,后来在PHP 5.3.0中增加到512)

另请注意, assoc必须是bool的值。

+0

感谢Gopi解释为什么我的对象深度是2,为什么我不能像PHP核心API那样传递NULL。 assoc NULL也不在核心api中。这个PHP核心API出eclipse PDT困惑了我,因为它也不同于php.net所说的json_decode – Talisin 2012-07-21 08:09:27

+0

@Talisin一定要接受答案并关闭这个问题如果任何答案满足你 – gopi1410 2012-07-21 08:17:02

+0

我会像往常一样让我找到为什么eclipse PHP核心api与php.net不同。 – Talisin 2012-07-21 08:20:15

2

深度假设是一个数字,因此(int)null == 0你正在为$深度传递0。从PHP手册512是默认值$深度http://php.net/manual/en/function.json-decode.php

+0

好的道理,但为什么PHP核心api将其设置为NULL而不是$ depth = 512? – Talisin 2012-07-21 08:00:22

+0

不应该告诉他默认值,你应该已经解释了“深度”的含义,虽然我不是downvoter。 – gopi1410 2012-07-21 08:01:11

+1

@Talisin深度是php应该在你的json对象中探索的递归深度。在你的情况2和更大的工作会很好。我在回答中提到了这些问题 – gopi1410 2012-07-21 08:02:26

1

Imho,面向对象的方法不值得在这种情况下自行车的发明。例如。只需从Yii框架的CJSON::decode method获得源代码(或者这是非常优秀的整个班级)。

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

您不能将NULL作为深度传递。所以你的json_decode()的别名是不正确的。

+0

谢谢,我不需要一个可行的解决方案,我需要了解为什么API核心设置为NULL,但如果将其设置为NULL,则会收到警告。 Yii框架的CJSON :: decode方法也忽略了深度,对吧? – Talisin 2012-07-21 07:54:36

+0

查看文档: 'json_decode(string $ json [,bool $ assoc = false [,int $ depth = 512 [,int $ options = 0]]])' 您不能传递NULL作为deth。 CJSON :: decode方法忽略了深度,是的,但是如果你不打算使用整个框架,只需在你的副本中添加一个参数到它的代码中。 – 2012-07-21 07:59:25

+0

感谢您指出php.net文档。最后,它帮助我发现我的eclipse PDT核心API与php.net doc不同。 – Talisin 2012-07-21 08:31:45

0

这不是你的函数的问题,如果它没有被传入,参数被设置为null。问题是因为你将null传递给json_decode作为深度。只需检查$assoc$depth是否为空,如果它们不是null,则将它们适当地传递给json_decode。此外,你应该明确$assoc是一个布尔值,并使用默认值。

function my_json_decode ($json, $assoc = false, $depth = null) { 
    if ($assoc && !$depth) 
     return json_decode($json, $assoc); 
    else if ($depth) 
     return json_decode($json, $assoc, $depth); 
    return json_decode($json); 
} 

但是,我不确定为什么你需要这样做,因为php-json库会为你处理这个问题。

+0

感谢您发布解决方案。编写自己的json类的原因是将json_last_error()方法包含到json_decode和json_encode方法中,并引发可能的json_error。 – Talisin 2012-07-21 08:29:53

相关问题