0

我已经将一些 XML 转换为 JSON 输出。现在,当我想循环值并想回显时。我很困惑,无法让它工作。我只需要是否有人可以给我想法或指出我错在哪里。

这是我的实际 JSON 输出。我已经在记事本中格式化了这个输出

{
   "@attributes":{
      "NMchs":"10"
   },
   "links":{
      "@attributes":{
         "fUrlBase":"http:\/\/synd.cricbuzz.com\/j2me\/1.0\/flags\/team_"
      }
   },
   "match":[
      {
         "@attributes":{
            "id":"3",
            "type":"T20",
            "srs":"Indian Premier League 2013",
            "mchDesc":"KOL vs CHN",
            "mnum":"26th Match",
            "vcity":"Kolkata",
            "vcountry":"India",
            "grnd":"Eden Gardens",
            "inngCnt":"1",
            "datapath":"http:\/\/synd.cricbuzz.com\/j2me\/1.0\/match\/2013\/2013_T20_LEAGUE\/KOL_CHN_APR20\/"
         },
         "state":{
            "@attributes":{
               "mchState":"inprogress",
               "status":"KOL elect to bat",
               "TW":"Kolkata",
               "decisn":"Batting",
               "addnStatus":"",
               "splStatus":""
            }
         },
         "Tm":[
            {
               "@attributes":{
                  "id":"63",
                  "Name":"Kolkata",
                  "sName":"KOL",
                  "flag":"0"
               }
            },
            {
               "@attributes":{
                  "id":"58",
                  "Name":"Chennai",
                  "sName":"CHN",
                  "flag":"0"
               }
            }
         ],
         "Tme":{
            "@attributes":{
               "Dt":"Apr 20 2013",
               "stTme":"10:30",
               "enddt":"Apr 20 2013"
            }
         },
         "mscr":{
            "inngsdetail":{
               "@attributes":{
                  "noofovers":"50",
                  "rrr":"0",
                  "crr":"6.49",
                  "cprtshp":"11(13)"
               }
            },
            "btTm":{
               "@attributes":{
                  "id":"63",
                  "sName":"KOL"
               },
               "Inngs":{
                  "@attributes":{
                     "desc":"Inns",
                     "r":"66",
                     "Decl":"0",
                     "FollowOn":"0",
                     "ovrs":"10.1",
                     "wkts":"4"
                  }
               }
            },
            "blgTm":{
               "@attributes":{
                  "id":"58",
                  "sName":"CHN"
               }
            }
         }
      }
   ]
}

输出中有 4-5 个类似的数据。下面是我试图在其中循环字段以使用变量回显的文件。

<?php
require_once("xml2json.php");

$testXmlFile = 'test.xml';

$xmlStringContents = file_get_contents($testXmlFile); 
$jsonContents = "";

$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
echo($jsonContents);

$obj = json_decode($jsonContents);

foreach ($obj->match as $match) {
    echo $match ->id,"<br>";
    echo $match ->attributes->type,"<br>";
    echo $match ->attributes->srs,"<br>";
    echo $match ->attributes->mchDesc,"<br>";
    echo $match ->attributes->mnum,"<br>";
    echo $match ->attributes->vcity,"<br>";
    echo $match ->attributes->vcountry,"<br>";
    echo $match ->attributes->grnd,"<br>";
    echo $match ->attributes->inngCnt,"<br>";
    echo $match ->attributes->state->attributes->mchState,"<br>";
    echo $match ->attributes->state->attributes->status,"<br>";
    echo $match ->attributes->state->attributes->TW,"<br>";
    echo $match ->attributes->state->attributes->decisn,"<br>";
    echo $match ->attributes->state->attributes->addnStatus,"<br>";
    echo $match ->attributes->state->attributes->splStatus,"<br>";
    echo $match ->attributes->Tm->attributes->id,"<br>";
    echo $match ->attributes->Tm->attributes->Name,"<br>";
    echo $match ->attributes->Tm->attributes->sName,"<br>";
    echo $match ->attributes->Tm->attributes->flag,"<br>";
    echo $match ->attributes->Tm->attributes->attributes->id,"<br>";
    echo $match ->attributes->Tm->attributes->attributes->Name,"<br>";
    echo $match ->attributes->Tm->attributes->attributes->sName,"<br>";
}
?>

我怎样才能做到这一点?

4

3 回答 3

0

您的 json 解码低于对结构的关注

 stdClass Object
(
   [@attributes] => stdClass Object
    (
        [NMchs] => 10
    )

   [links] => stdClass Object
    (
        [@attributes] => stdClass Object
            (
                [fUrlBase] => http://synd.cricbuzz.com/j2me/1.0/flags/team_
            )

    )

    [match] => Array
    (
        [0] => stdClass Object
            (
                [@attributes] => stdClass Object
                    (
                        [id] => 3
                        [type] => T20
                        [srs] => Indian Premier League 2013
                        [mchDesc] => KOL vs CHN
                        [mnum] => 26th Match
                        [vcity] => Kolkata
                        [vcountry] => India
                        [grnd] => Eden Gardens
                        [inngCnt] => 1
                        [datapath] => http://synd.cricbuzz.com/j2me/1.0/match/2013/2013_T20_LEAGUE/KOL_CHN_APR20/
                    )

                [state] => stdClass Object
                    (
                        [@attributes] => stdClass Object
                            (
                                [mchState] => inprogress
                                [status] => KOL elect to bat
                                [TW] => Kolkata
                                [decisn] => Batting
                                [addnStatus] => 
                                [splStatus] => 
                            )

                    )

                [Tm] => Array
                    (
                        [0] => stdClass Object
                            (
                                [@attributes] => stdClass Object
                                    (
                                        [id] => 63
                                        [Name] => Kolkata
                                        [sName] => KOL
                                        [flag] => 0
                                    )

                            )

                        [1] => stdClass Object
                            (
                                [@attributes] => stdClass Object
                                    (
                                        [id] => 58
                                        [Name] => Chennai
                                        [sName] => CHN
                                        [flag] => 0
                                    )

                            )

                    )

                [Tme] => stdClass Object
                    (
                        [@attributes] => stdClass Object
                            (
                                [Dt] => Apr 20 2013
                                [stTme] => 10:30
                                [enddt] => Apr 20 2013
                            )

                    )

                [mscr] => stdClass Object
                    (
                        [inngsdetail] => stdClass Object
                            (
                                [@attributes] => stdClass Object
                                    (
                                        [noofovers] => 50
                                        [rrr] => 0
                                        [crr] => 6.49
                                        [cprtshp] => 11(13)
                                    )

                            )

                        [btTm] => stdClass Object
                            (
                                [@attributes] => stdClass Object
                                    (
                                        [id] => 63
                                        [sName] => KOL
                                    )

                                [Inngs] => stdClass Object
                                    (
                                        [@attributes] => stdClass Object
                                            (
                                                [desc] => Inns
                                                [r] => 66
                                                [Decl] => 0
                                                [FollowOn] => 0
                                                [ovrs] => 10.1
                                                [wkts] => 4
                                            )

                                    )

                            )

                        [blgTm] => stdClass Object
                            (
                                [@attributes] => stdClass Object
                                    (
                                        [id] => 58
                                        [sName] => CHN
                                    )

                            )

                    )

            )

    )

 )

你应该改变 foreach 代码

  foreach ($obj->match as $match) {
    echo $match ->{'@attributes'}->type,"<br>";
    echo $match ->{'@attributes'}->srs,"<br>";
    echo $match ->{'@attributes'}->mchDesc,"<br>";
    echo $match ->{'@attributes'}->mnum,"<br>";
    echo $match ->{'@attributes'}->vcity,"<br>";
    echo $match ->{'@attributes'}->vcountry,"<br>";
    echo $match ->{'@attributes'}->grnd,"<br>";
    echo $match ->{'@attributes'}->inngCnt,"<br>";
    echo $match ->state->{'@attributes'}->mchState,"<br>";
    echo $match ->state->{'@attributes'}->status,"<br>";
    echo $match ->state->{'@attributes'}->TW,"<br>";
    echo $match ->state->{'@attributes'}->decisn,"<br>";
    echo $match ->state->{'@attributes'}->addnStatus,"<br>";
    echo $match ->state->{'@attributes'}->splStatus,"<br>";
    foreach($match ->Tm as $Tm){
       echo $Tm->{'@attributes'}->id,"<br>";
       echo $Tm->{'@attributes'}->Name,"<br>";
       echo $Tm->{'@attributes'}->sName,"<br>";
       echo $Tm->{'@attributes'}->flag,"<br>";
   }
}

输出为 html

 T20<br>Indian Premier League 2013<br>KOL vs CHN<br>26th Match<br>Kolkata<br>India<br>Eden Gardens<br>1<br>inprogress<br>KOL elect to bat<br>Kolkata<br>Batting<br><br><br>63<br>Kolkata<br>KOL<br>0<br>58<br>Chennai<br>CHN<br>0<br>

编辑:

请参阅https://cdn.anonfiles.com/1366532327809.txt的 json 解码,它表明您需要 put$obj->mchdata->match而不是$obj->match将此代码更改为:

 foreach ($obj->mchdata->match as $match) {
    echo @$match ->{'@attributes'}->type,"<br>";
    echo @$match ->{'@attributes'}->srs,"<br>";
    echo @$match ->{'@attributes'}->mchDesc,"<br>";
    echo @$match ->{'@attributes'}->mnum,"<br>";
    echo @$match ->{'@attributes'}->vcity,"<br>";
    echo @$match ->{'@attributes'}->vcountry,"<br>";
    echo @$match ->{'@attributes'}->grnd,"<br>";
    echo $match ->{'@attributes'}->inngCnt,"<br>";
    echo @$match ->state->{'@attributes'}->mchState,"<br>";
    echo @$match ->state->{'@attributes'}->status,"<br>";
    echo @$match ->state->{'@attributes'}->TW,"<br>";
    echo @$match ->state->{'@attributes'}->decisn,"<br>";
    echo @$match ->state->{'@attributes'}->addnStatus,"<br>";
    echo @$match ->state->{'@attributes'}->splStatus,"<br>";
    foreach($match ->Tm as $Tm){
       echo @$Tm->{'@attributes'}->id,"<br>";
       echo @$Tm->{'@attributes'}->Name,"<br>";
       echo @$Tm->{'@attributes'}->sName,"<br>";
       echo @$Tm->{'@attributes'}->flag,"<br>";
   }
}    

输出为 html

T20<br>Indian Premier League 2013<br>DEL vs MUM<br>28th Match<br>Delhi<br>India<br>Feroz Shah Kotla<br>0<br>preview<br>Starts on Apr 21 at 10:30 GMT<br><br><br><br><br>61<br>Delhi<br>DEL<br>0<br>62<br>Mumbai<br>MUM<br>0<br>T20<br>Indian Premier League 2013<br>MOH vs PUNE<br>29th Match<br>Mohali<br>India<br>Punjab Cricket Association Stadium<br>0<br>preview<br>Starts on Apr 21 at 14:30 GMT<br><br><br><br><br>65<br>Punjab<br>MOH<br>0<br>183<br>Pune<br>PUNE<br>0<br>T20<br>Indian Premier League 2013<br>BLR vs JAI<br>27th Match<br>Bengaluru<br>India<br>M.Chinnaswamy Stadium<br>2<br>complete<br>Royal Challengers Bangalore won by 7 wkts<br>Bangalore<br>Fielding<br><br><br>59<br>Bangalore<br>BLR<br>0<br>64<br>Rajasthan<br>JAI<br>0<br>T20<br>Indian Premier League 2013<br>KOL vs CHN<br>26th Match<br>Kolkata<br>India<br>Eden Gardens<br>2<br>complete<br>Chennai won by 4 wkts<br>Kolkata<br>Batting<br><br><br>63<br>Kolkata<br>KOL<br>0<br>58<br>Chennai<br>CHN<br>0<br>T20<br>Indian Premier League 2013<br>BLR vs JAI<br>27th Match<br><br><br><br>2<br>Result<br>Royal Challengers Bangalore won by 7 wkts<br><br><br><br><br>59<br>Bangalore<br>BLR<br>0<br>64<br>Rajasthan<br>JAI<br>0<br>T20<br>Kenya tour of Namibia 2013<br>KEN vs NED<br>Only T20I<br><br><br><br>2<br>Result<br>Kenya won by 5 wkts<br><br><br><br><br>14<br>Ken<br>KEN<br>1<br>24<br>NED<br>NED<br>1<br>
于 2013-04-20T12:51:59.853 回答
0

假设您的 XML 转换为正确的 JSON,并且 json_decode 返回 NULL 以外的内容。你可以用这个。此外,由于您在 JSON 中留下了“@”字符,因此您必须使用 PHP 特殊语法来检索该属性,因为 @ 符号对 PHP 变量无效。如果您从属性中删除“@”,那么您将能够使用 $match->attributes。

<?php
    $obj = json_decode( $jsonContents );

    foreach ( $obj->match as $match )
    {
        echo $match->{'@attributes'}->id . '<br>';
        echo $match->{'@attributes'}->type . "<br>";
        echo $match->{'@attributes'}->srs . "<br>";
        echo $match->{'@attributes'}->mchDesc . "<br>";
        echo $match->{'@attributes'}->mnum . "<br>";
        echo $match->{'@attributes'}->vcity . "<br>";
        echo $match->{'@attributes'}->vcountry . "<br>";
        echo $match->{'@attributes'}->grnd . "<br>";
        echo $match->{'@attributes'}->inngCnt . "<br>";
        echo $match->state->{'@attributes'}->mchState . "<br>";
        echo $match->state->{'@attributes'}->status . "<br>";
        echo $match->state->{'@attributes'}->TW . "<br>";
        echo $match->state->{'@attributes'}->decisn . "<br>";
        echo $match->state->{'@attributes'}->addnStatus . "<br>";
        echo $match->state->{'@attributes'}->splStatus . "<br>";
        foreach ( $match->Tm as $Tm )
        {
            echo $Tm->{'@attributes'}->id . "<br>";
            echo $Tm->{'@attributes'}->Name . "<br>";
            echo $Tm->{'@attributes'}->sName . "<br>";
            echo $Tm->{'@attributes'}->flag . "<br>";
        }
    }
?>

这是我用来测试的一些示例 JSON(取自上面并已修复)。

<?php
$jsonContents = <<<EOT
{
   "@attributes":{
      "NMchs":"10"
   },
   "links":{
      "@attributes":{
         "fUrlBase":"http:\/\/synd.cricbuzz.com\/j2me\/1.0\/flags\/team_"
      }
   },
   "match":[
      {
         "@attributes":{
            "id":"3",
            "type":"T20",
            "srs":"Indian Premier League 2013",
            "mchDesc":"KOL vs CHN",
            "mnum":"26th Match",
            "vcity":"Kolkata",
            "vcountry":"India",
            "grnd":"Eden Gardens",
            "inngCnt":"1",
            "datapath":"http:\/\/synd.cricbuzz.com\/j2me\/1.0\/match\/2013\/2013_T20_LEAGUE\/KOL_CHN_APR20\/"
         },
         "state":{
            "@attributes":{
               "mchState":"inprogress",
               "status":"KOL elect to bat",
               "TW":"Kolkata",
               "decisn":"Batting",
               "addnStatus":"",
               "splStatus":""
            }
         },
         "Tm":[
            {
               "@attributes":{
                  "id":"63",
                  "Name":"Kolkata",
                  "sName":"KOL",
                  "flag":"0"
               }
            },
            {
               "@attributes":{
                  "id":"58",
                  "Name":"Chennai",
                  "sName":"CHN",
                  "flag":"0"
               }
            }
         ],
         "Tme":{
            "@attributes":{
               "Dt":"Apr 20 2013",
               "stTme":"10:30",
               "enddt":"Apr 20 2013"
            }
         },
         "mscr":{
            "inngsdetail":{
               "@attributes":{
                  "noofovers":"50",
                  "rrr":"0",
                  "crr":"6.49",
                  "cprtshp":"11(13)"
               }
            },
            "btTm":{
               "@attributes":{
                  "id":"63",
                  "sName":"KOL"
               },
               "Inngs":{
                  "@attributes":{
                     "desc":"Inns",
                     "r":"66",
                     "Decl":"0",
                     "FollowOn":"0",
                     "ovrs":"10.1",
                     "wkts":"4"
                  }
               }
            },
            "blgTm":{
               "@attributes":{
                  "id":"58",
                  "sName":"CHN"
               }
            }
         }
      }
   ]
}
EOT;
?>
于 2013-04-20T13:00:31.717 回答
0

我正在详细说明:

如果您想将整个对象显示为带有分隔符的字符串,您可以使用递归函数来实现,例如

$obj = json_decode($json);

function objectToString($obj, $delimiter = PHP_EOL) {
    foreach($obj as $key=>$val) {

        if(is_object($val) || is_array($val)) {
           objectToString($val, $delimiter);
        } else {
            echo $key . '=> ' .$val . $delimiter;
        }
    }
}

objectToString($obj, '<br />');

但它并不是真正有用或仅用于调试,使用print_r函数更容易。

于 2013-04-20T13:08:31.553 回答