我在Yii框架中工作。我有以下功能 -
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 :(得分: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"}]}