2012-07-21 93 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

相關問題