1

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?

1 Answer 1

3

Any reason this won't work?

$json = json_decode($json, TRUE);
$results = $json['bossresponse']['web']['results'];
foreach($results as $result) {
    echo $result['title'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

viola that is work ...with this the code is simple but powerfull...thanks very much...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.