简体   繁体   中英

Parsing JSON file using json_decode

I am having an issue with parsing my JSON file with the PHP function json_decode. I am currently seeing only the last object in the nested JSON array (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.

my JSON file:

{  
    "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:

$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
                   }
                ]
             }
          ]
       }
    ]
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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