解码JSON feed有一个@字符- PHP


Decoding JSON feed that has an @ character - PHP

下面是我的JSON示例:

[  
   {  
      "@attributes":{  
         "name":"Stack Overflow Movies ",
         "root":"http:'/'/www1.stackovermovies.com'/",
         "id":"1",
         "address":"Liverpool",
         "postcode":"PZ203434"
      },
      "films":{  
         "film":[  
            {  
               "@attributes":{  
                  "synopsis":"Description goes here.'r'n",
                  "poster":"http:'/'/www1.stackoverflowmovies.com'/posters'/HO00003702.jpg",
                  "cast":"Wayne Max",
                  "director":"",
                  "length":"125 mins",
                  "title":"X-Men (2016)",
                  "rating":"12A",
                  "release":"19'/11'/2016"
               },
               "shows":{  
                  "show":{  
                     "@attributes":{  
                        "date":"Sat 19 Nov",
                        "time":"17:00"
                     }
                  }
               }
        },

我试着解码它:

变量$feed包含了上面的JSON。

我试图获取name属性

echo $feed->'@attributes[0]'->name;,但由于@字符,这不起作用。有解决办法吗?我试着查看CDATA,但无法确定这是否是正确的解决方案。

使用json_decode($json, true)获取数组而不是对象。然后你可以像这样访问你的字段:

$feed[0]['@attributes']['name'];

访问它的一种方法是为对象中的键提供字符串值,如下所示:

$decoded->{'@attributes'};