簡體   English   中英

解碼對象內部的json對象

[英]decoding json object inside object inside object

我正在嘗試使用json_decode()解碼json文件,第二個參數為true ,它是從file_get_contents()獲得的json字符串之后的。 像下面。

json_decode(file_get_contents($dir), true);

我已經確保文件中的目錄正確,並存儲在$dir的json文件中。

{
"must": {
    "title": {
        "tag": "<!--section title-->",
        "content": null,
        "with-content": true
    },
    "class": {
        "tag": "<!--section class-->",
        "content": null,
        "with-content": true
    }
}

但是當我嘗試使用以下腳本獲取json文件時。

if(is_file_ready($dir)) {
    $set = json_decode(file_get_contents($dir), true);
    echo file_get_contents($dir);   

    if(isset($set['must'])) {
        foreach($set['must'] as $node) {
        echo $node['tag'];
    }
}

它僅顯示第一個echo如下所示, echo $node['tag']顯示任何內容。

{“必須”:{“標題”:{“標簽”:“”,“內容”:null,“有內容”:true},“類別”:{“標簽”:“”,“內容”:null ,“ with-content”:true}}}

當我將echo $node['tag']更改為print_r($node['tag'])它顯示如下。

數組([tag] => [content] => [with-content] => 1)Array([tag] => [content] => [with-content] => 1)

我的問題是,為什么tag返回null而內容也返回? <!-- -->讀為與html相同的json的注釋嗎? 還是我做錯了什么?

感謝您的任何更正。

您的代碼工作正常。 您只需嘗試在瀏覽器中運行它。 如果打開開發工具(f12),您將在html中看到這些值顯示為注釋,因此看不到它們。

如果使用郵差運行腳本,則會看到輸出正常打印。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM