3

I have following codes in my controller:

public function actionCabinet($id){

    $this->render('cabinet', array('model'=>$this->loadJson($id)) );

}

    public function loadJson($id)
    {

        $jsonfile=ChForms::model()->findByPk($id, array("select"=>"json"));
        $decodedJson=json_decode($jsonfile, true);
        return $decodedJson;

    }

Data is saved in json field in ChForm in json format. I am going to convert it to array. When I run this application, it displays following error message:

json_decode() expects parameter 1 to be string, object given

How can I fix this error?

phpdev
  • 511
  • 4
  • 22

2 Answers2

1

Please try this,

 public function loadJson($id)
{

    $jsonfile=ChForms::model()->findByPk($id);
    $decodedJson=json_decode($jsonfile->json, true);
    return $decodedJson;

}
Arya
  • 504
  • 2
  • 8
  • 31
-1

You can use json_encode like below,

public function loadJson($id)
{

    $jsonfile=ChForms::model()->findByPk($id, array("select"=>"json"));
    $decodedJson=json_encode($jsonfile, true);
    return $decodedJson;

}
KasunSm
  • 9
  • 2