我已经将一些 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>";
}
?>
我怎样才能做到这一点?