0

I need to access a json $json_f from my class helper

<?php 
    class helper
    {
        protected $json_f = '{"fruits" : [
         {"id":0, "name":"Apple"},
         {"id":1, "name":"Orange"},
         {"id":2, "name":"Mango"}
       ]}';

     public function testc()
     {
        $data = json_decode($this->json_f);

        foreach ($data->fruits as $key) {
          echo '<p>';
          echo 'id : ' . htmlspecialchars($key->id) . '<br />';
          echo 'name : ' . htmlspecialchars($key->name) . '<br />';
          echo '</p>';
        }
     }
    }

?>

but im getting

this error: Trying to get property of non-object

below i have same in plain php

and which is working

<?php
$json_f = '{"fruits" : [
             {"id":0, "name":"Apple"},
             {"id":1, "name":"Orange"},
             {"id":2, "name":"Mango"}
         ]}';

$data = json_decode($json_f);

foreach ($data->fruits as $note) {
  echo '<p>';
  echo 'text : ' . htmlspecialchars($note->id) . '<br />';
  echo 'key : ' . htmlspecialchars($note->name) . '<br />';
}
?>
4
  • 1
    Feel free to put your json into jsonlint.com . its a json validator that will tell you where your errors occur. Commented Oct 20, 2015 at 11:56
  • @Alex got ur point, i have updated...thanx Commented Oct 20, 2015 at 12:02
  • @Rahautos thanks... got it working..... Commented Oct 20, 2015 at 12:06
  • @Alex ..is it.. can u plz tell me where .... Commented Oct 20, 2015 at 12:09

2 Answers 2

3

Your json string is not valid.

Try this:

protected $json_f = '{"fruits" : [
     {"id":0, "name":"Apple"},
     {"id":1, "name":"Orange"},
     {"id":2, "name":"Mango"}
   ]}';

Note that I removed the commas after each fruit.

After this, running

$d = new helper();
$d->testc();

// outputs
// <p>id : 0<br />name : Apple<br /></p><p>id : 1<br />name : Orange<br /></p><p>id : 2<br />name : Mango<br /></p>

Everything should work out fine.

Sign up to request clarification or add additional context in comments.

1 Comment

there are issues even after making the json valid?
1

Try this code :-

Validate your json before use

 <?php 
        class helper
        {
           protected $json_f = '{"fruits" : [
         {"id":0, "name":"Apple"},
         {"id":1, "name":"Orange"},
         {"id":2, "name":"Mango"}
       ]}';

         public function testc()
         {
            $data = json_decode($this->json_f);
            foreach ($data->fruits as $key) {
              echo '<p>';
              echo 'id : ' . htmlspecialchars($key->id) . '<br />';
              echo 'name : ' . htmlspecialchars($key->name) . '<br />';
              echo '</p>';
            }
         }
        }
        $helper = new helper;
        $helper->testc();

    ?>

Run code

Comments

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.