简体   繁体   English

使用 json_decode 解析 JSON 文件

[英]Parsing JSON file using json_decode

I am having an issue with parsing my JSON file with the PHP function json_decode.我在使用 PHP 函数 json_decode 解析我的 JSON 文件时遇到问题。 I am currently seeing only the last object in the nested JSON array (BANNER2) .我目前只看到嵌套 JSON 数组 (BANNER2) 中的最后一个对象 I realize i am using duplicate keys in the JSON file but i am clueless on how to structure the JSON file in a different way.我意识到我在 JSON 文件中使用了重复的键,但我对如何以不同的方式构造 JSON 文件一无所知。

my JSON file:我的 JSON 文件:

{  
    "project_filename":"testzip",
    "data":[  
       {  
          "title":"Quebec",
          "displayTag":"H1",
          "css":"",
          "type":"header",
          "display_title":"",
          "data":[  
             {  
                "title":"BANNER1",
                "displayTag":"h2",
                "css":"hidden",
                "type":"collapse",
                "display_title":"",
                "data":[  
                   {  
                      "title":"160x600",
                      "displayTag":"p",
                      "display_title":"DESCRIPTION",
                      "filename":"300x250",
                      "type":"banner",
                      "source":"pages/300x250/index.html",
                      "width":"300",
                      "height":"250",
                      "controls":true
                   }
                ],
                "title":"BANNER2",
                "displayTag":"h2",
                "css":"hidden",
                "type":"collapse",
                "display_title":"",
                "data":[  
                   {  
                      "title":"160x600",
                      "displayTag":"p",
                      "display_title":"DESCRIPTION",
                      "filename":"300x250",
                      "type":"banner",
                      "source":"pages/300x250/index.html",
                      "width":"300",
                      "height":"250",
                      "controls":true
                   }
                ]
             }
          ]
       }
    ]
 }

my PHP file:我的 PHP 文件:

$JSONdata = json_decode($data, true);

foreach ($JSONdata['data'] as $key => $dt) {
    foreach ($dt['data'] as $data) {

    // use the JSON values in the $dt variable and do stuff with it

The two banners should be separate objects in the array, not duplicate keys in a single object.两个横幅应该是数组中的单独对象,而不是单个对象中的重复键。 Object keys have to be unique.对象键必须是唯一的。

{  
    "project_filename":"testzip",
    "data":[  
       {  
          "title":"Quebec",
          "displayTag":"H1",
          "css":"",
          "type":"header",
          "display_title":"",
          "data":[  
             {  
                "title":"BANNER1",
                "displayTag":"h2",
                "css":"hidden",
                "type":"collapse",
                "display_title":"",
                "data":[  
                   {  
                      "title":"160x600",
                      "displayTag":"p",
                      "display_title":"DESCRIPTION",
                      "filename":"300x250",
                      "type":"banner",
                      "source":"pages/300x250/index.html",
                      "width":"300",
                      "height":"250",
                      "controls":true
                   }
                ]
             },{
                "title":"BANNER2",
                "displayTag":"h2",
                "css":"hidden",
                "type":"collapse",
                "display_title":"",
                "data":[  
                   {  
                      "title":"160x600",
                      "displayTag":"p",
                      "display_title":"DESCRIPTION",
                      "filename":"300x250",
                      "type":"banner",
                      "source":"pages/300x250/index.html",
                      "width":"300",
                      "height":"250",
                      "controls":true
                   }
                ]
             }
          ]
       }
    ]
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM