-1

我有以下结构的 json 数据

{

    "page": 0,
    "items": 43,
    "total": 43,
    "incentiveItems": {
        "incentiveItem": [
            {
                "@id": "1111",
                "name": "...",
                "program": {
                    "@id": "6765",
                    "$": "NAME"
                },
                "admedia": {
                    "admediumItem": {
                        ...

还有更多,为了解析 php 中的数据,我做了一个简单的 foreach ......

$obj = json_decode($jsonData);
echo $obj->items;
echo '<table><tr><td>Nome</td><td>Descrizione</td><td>Codice</td></tr>';
foreach($obj->incentiveItems->incentiveItem as $programma  ) {

所以,问题是我不能调用“$”:“NAME”值,因为如果我使用(例如)

$programma->program->$
$programma->program['$']
$obj->incentiveItems->incentiveItem->program->$ or ['$']

它崩溃了

@id 字段也是如此。

有人能帮我吗?谢谢

4

2 回答 2

0

Tyr 将 json 解码为关联数组:

$obj = json_decode($jsonData, true);
echo $obj['items'];
echo '<table><tr><td>Nome</td><td>Descrizione</td><td>Codice</td></tr>';
foreach($obj['incentiveItems']['incentiveItem'] as $programma  ) {
  echo $programma['@id'];
于 2013-05-28T15:57:39.397 回答
0

我相信您可以使用单引号和大括号来访问它

$obj->incentiveItems->incentiveItem->program->{'$'}

$obj->incentiveItems->incentiveItem->program->{'@id'}
于 2013-05-28T15:57:46.367 回答