2012-12-20 42 views
0

我正在Yii框架中工作。我有以下功能 -在yii如何發送所需格式的json

public function actionGetPublicPoll() 
{ 
    $model=new poll(); 
    $polltype=Poll::model()->findAllByAttributes(array("pollTypeId"=>1)); 
    foreach ($polltype as $poll) 
    { 
     if($poll->isActive==1 && $poll->isPublished==0) 
     { 
      echo "pollId is=".$poll->pollId; 
      $Id=$poll->pollId; 
      $option=Polloption::model()->findAllByAttributes(array("pollId"=>$poll->pollId)); 

      foreach ($option as $option1) 
      { 
       echo $option1->optionId."</br>"; 
       echo $option1->option; 
       $optionList[]=$option1->option; 
      } 
     } 
    } 
    echo CJSON::encode(array("PollId"=>$Id,"Question"=>$poll->pollQuestion,"options"=>$optionList)); 
} 

所以我發送pollQuestion及其json格式選項。從上面看,我越來越原樣

{"PollId":"3","Question":"Which is the biggest district in india ","options":["sachin tendulakar","Yuwraj singh","Rohit sharma","Mahendrasing dhoni"]} 

輸出,但我想在JSON格式:

{"PollId":"3","Question":"Which is the biggest district in india ","options":["option":"sachin tendulakar","option":"Yuwraj singh","option":"Rohit sharma","option":"Mahendrasing dhoni"]} 

所以我需要修改?請幫幫我....

回答

0

更改

$optionList[]=$option1->option;

$optionList[]=array('option'=>$option1->option);

................ ................

foreach ($option as $option1) 
{ 
       echo $option1->optionId."</br>"; 
       echo $option1->option; 
       $optionList[]=array('option'=>$option1->option); 
} 

.............. ..............

你會得到JSON作爲

{"PollId":"3","Question":"Which is the biggest district in india ", 
"options":[{"option":"sachin tendulakar"},{"option":"Yuwraj singh"},{"option":"Rohit sharma"},{"option":"Mahendrasing dhoni"}]} 
+0

Thanx先生的工作 – user1636115

+0

歡迎您.. – nu6A

相關問題