Display Json Response In Yii

Hello Everbody, First i wanna say, happy nu year. and happy nu problem

i have trouble in show json response,

i success to get data from server, i use file_get_content,

firstly i have model form, it contain 1 textfield dan 1 button submit, i fill textfield with transaction code (ex: 123456) and click submit button, then it will response json data, it successfull, but, the question,

i want to display the result into cgridview, i tried to convert it into carraydataprovider, but it doesnt work…

actually, i still confuse, what must i do, i just want to show data in nice view (ex like table, cgridview, cdetailview etc).

please give me some advice, thanks a lot

How have you tried to convert the dataset? And in which way didn’t it work?

i have json data response from server like this, ex : {"name":"minora", "hobby":"watching", "status":"married"}

and i had convert it into carraydata with code like this,

$data = {"name":"minora", "hobby":"watching", "status":"married"};

$rawData = $data;

$dataProvider=new CArrayDataProvider($rawData, array(

	            'sort'=>array(


	            'attributes'=>array(


	                'name','hobby','status',


	    ),


	    ),


	        


	    ));

when i tried to print_r the result, use code like this:

$getdata = $dataProvider->getData();

print_r($getdata);

it gives me error :

PHP warning

array_slice() expects parameter 1 to be array, string given

C:\xampp\yii\framework\web\CArrayDataProvider.php(95)

083 /**

084 * Fetches the data from the persistent data storage.

085 * @return array list of data items

086 */

087 protected function fetchData()

088 {

089 if(($sort=$this->getSort())!==false && ($order=$sort->getOrderBy())!=’’)

090 $this->sortData($this->getSortDirections($order));

091

092 if(($pagination=$this->getPagination())!==false)

093 {

094 $pagination->setItemCount($this->getTotalItemCount());

095 return array_slice($this->rawData, $pagination->getOffset(), $pagination->getLimit());

096 }

097 else

098 return $this->rawData;

099 }

100

101 /**

102 * Fetches the data item keys from the persistent data storage.

103 * @return array list of data item keys.

104 */

105 protected function fetchKeys()

106 {

107 if($this->keyField===false)

what must i do?

please give me clue for solve this,thanks a lot

How about CJSON.decode()? Also I don’t see how your sample data qualifies for a data provider or in fact any listing widget. It really looks like a single entry, not a set of entries.

thanks for ur response,this program, get input datacode from user, and when user click check button, it will return/result single json data, only like {"datacode":"123", "message":"hello","status":"good"}

this my model,

<?php

class Mydata extends CFormModel {

public &#036;data_code;


public &#036;errorcode, &#036;message, &#036;name, &#036;status;








public function rules()


{


	return array(


	array('data_code','required'),


	array('data_code, 'numerical'),





	);


}





public function attributeLabels()


{


	return array(


	);


}

}

?>

my controller,

public function actionData()


{


	


	&#036;model=new Mydata();








	// collect user input data


	if(isset(&#036;_POST['Mydata']))


	{


		&#036;model-&gt;attributes=&#036;_POST['Mydata'];


		


		&#036;data[&quot;username&quot;] = &quot;dummy&quot;;


		&#036;data[&quot;password&quot;] = &quot;dummy&quot;;


		&#036;data[&quot;data_code&quot;] = &#036;_POST['Mydata']['data_code'];


		


		&#036;post =CJSON::encode(&#036;data);


		


		


		&#036;url = &quot;http://www.mydata.com&quot;;


		


		&#036;ch = curl_init();


		curl_setopt(&#036;ch, CURLOPT_URL, &#036;url);


		curl_setopt(&#036;ch, CURLOPT_POST, 1);


		curl_setopt(&#036;ch, CURLOPT_POSTFIELDS, &#036;post);


		&#036;output = curl_exec(&#036;ch);


		curl_close(&#036;ch);


		


		


	        if(&#036;output&#33;=&quot;null&quot;)


		&#036;obj=CJSON::decode(&#036;output, true); //result = Array ( [datacode] =&gt; 123 [message]=&gt;hello [name] =&gt; tata  [status] =&gt; good )








               &#036;rawData = &#036;obj;


	   &#036;dataProvider=new CArrayDataProvider(&#036;rawData, array(


	    


	   		        'sort'=&gt;array(


	   		            'attributes'=&gt;array(


	   		                'errorcode','message','name','status',


	   ),


	   ),


	   


	   ));





                //i tried to show array in indexMydata view


                &#036;this-&gt;render('indexMydata',array('model'=&gt;&#036;model));

}

		// display the mydata form


		&#036;this-&gt;render('mydataForm',array('model'=&gt;&#036;model));





		}

my View (mydataForm)

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'Mydata-form',


'enableClientValidation'=&gt;false,

)); ?>

&lt;div class=&quot;row&quot;&gt;


	


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'datacode'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'datacode'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'datacode'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Check'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

my View (indexMydata)

<?php

$this->widget(‘zii.widgets.grid.CGridView’, array(

'dataProvider'=&gt;&#036;dataProvider,


	'columns'=&gt;array(


		array(


			'name'=&gt;'errorcode',


			'value'=&gt;'&#036;data[&quot;errorcode&quot;]',


		),


		array(


			'name'=&gt;'message',


			'value'=&gt;'&#036;data[&quot;message&quot;]',


		),


	),


));

?>

its now give me an error

PHP notice

Undefined variable: dataProvider

i hope u can help me to solve my problem, must i learn ajax or what??

actually, i still confusing, how to show other view when i click check/submit button…hehe…

thanks a lot…

after searching and googling, i have solved this problem…

but new trouble comes…

how to show the result when i click check button…hehhee

but i will try to solve it…

if anybody there can help me…please…thanks

Hi meechamp,

How to show {"datacode":"123", "message":"hello","status":"good"} in view???