Reputation: 2339
i try to parsing json with format like this
{
"bossresponse":{
"responsecode":"200",
"web":{
"start":"0",
"count":"50",
"totalresults":"88300000",
"results":[
{
"date":"",
"clickurl":"http:\/\/naruto.viz.com\/",
"url":"http:\/\/naruto.viz.com\/",
"dispurl":"naruto<\/b>.viz.com",
"title":"NARUTO<\/b> Shippuden - OFFICIAL U.S. Site - Watch the Anime ...",
"abstract":"Check out the latest discussions! TOPICS. AUTHOR"
},
{
"date":"",
"clickurl":"http:\/\/naruto.com\/",
"url":"http:\/\/naruto.com\/",
"dispurl":"naruto<\/b>.com",
"title":"NARUTO<\/b>",
"abstract":""
}
]
}
}
}
and when i parse with this function
$json = file_get_contents("test.json");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if (is_array($val)) {
//echo "$key:\n";
if($key == "web"){
foreach($val as $key1 => $results){
if(is_array($results)){
if($key1 == "results"){
foreach($results as $key2 => $v){
if (is_array($v)) {
foreach ($v as $v1) {
echo $v[title];
}
}
}
//echo $results;
}
}
}
//echo $val;
}
} else {
//echo "$key => $val\n";
}
}
it's successfull but i think my code is crazy code,you can look foreach and foreach very much is not good to maintenance. i want to implement in OOP like with class... can you help me how i can do that?any suggestion?
Upvotes: 1
Views: 1646
Reputation: 10781
Any reason this won't work?
$json = json_decode($json, TRUE);
$results = $json['bossresponse']['web']['results'];
foreach($results as $result) {
echo $result['title'];
}
Upvotes: 3