首页 文章

Json encocde数组的混合与仅在数组中转换的对象

提问于
浏览
1

我的php函数返回一个我转换成json使用的数组

$req = json_encode($questionData);

然后将此传递给其他函数并解码 $res = json_decode($req);

它返回一个数组,包括数组格式的所有索引 . 即使在请求数组中,$ questionData在某个索引上也有对象,那些在jason编码和解码执行后应该在对象中 .

我想获得具有arrya和object数据混合的相同数组 .

下面是请求数组

Array
(
    [success] => 1
    [pagename] => RC1_PGR_Questions
    [result] => Array
        (
            [CurrentInsuranceStatus] => Array
                (
                    [QuestionData] => Platform_Form_Page_Question Object
                        (
                            [_answer] => 
                            [_answerOptions] => Array
                                (
                                    [0] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340280
                                            [_hideAnswer] => 1
                                            [_order] => 110
                                            [_promptText] => Yes
                                            [_value] => Y
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [1] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340290
                                            [_hideAnswer] => 
                                            [_order] => 120
                                            [_promptText] => Yes, on parent's policy
                                            [_value] => Parents
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [2] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340300
                                            [_hideAnswer] => 
                                            [_order] => 130
                                            [_promptText] => Yes, insured through employer
                                            [_value] => Company
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [3] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340310
                                            [_hideAnswer] => 
                                            [_order] => 140
                                            [_promptText] => No, policy lapsed/expired
                                            [_value] => Lapsed
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [4] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340320
                                            [_hideAnswer] => 
                                            [_order] => 150
                                            [_promptText] => No, never been insured
                                            [_value] => Never
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [5] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340330
                                            [_hideAnswer] => 
                                            [_order] => 160
                                            [_promptText] => No, I didn't have a vehicle to insure
                                            [_value] => First
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [6] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340340
                                            [_hideAnswer] => 
                                            [_order] => 170
                                            [_promptText] => No, been deployed/overseas in the military
                                            [_value] => Military
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [7] => Platform_Form_Page_Question_AnswerOption Object
                                        (
                                            [_answerKey] => 340350
                                            [_hideAnswer] => 
                                            [_order] => 180
                                            [_promptText] => No, other reason
                                            [_value] => Other
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                )

                            [_answersDivID] => html_CurrentInsuranceStatus
                            [_phoneAreaField] => 
                            [_attributeName] => CurrentInsuranceStatus
                            [_criteriaBasedQuestion] => 1
                            [_dateField] => 
                            [_eventHandler] => 
                            [_fieldType] => select
                            [_hideQuestion] => 1
                            [_monthField] => 
                            [_phoneNumberField] => 
                            [_order] => 10000
                            [_pageAttributes] => stdClass Object
                                (
                                    [additionalLabel] => 
                                    [answerStyle] => 
                                    [colNum] => 
                                    [colWidth] => 
                                    [cssclass] => 
                                    [defaultValue] => 
                                    [isMultiSelect] => 
                                    [maxLength] => 
                                    [min] => 
                                    [questionSkinStyle] => 
                                    [toolTip] => 
                                )

                            [_phonePreFixField] => 
                            [_questionDivID] => prompt_CurrentInsuranceStatus
                            [_questionKey] => 7169010
                            [_required] => 1
                            [_text] => Current insurance status
                            [_yearField] => 
                            [_errorLog] => Array
                                (
                                )

                        )

                )

        )

)

响应数组

stdClass Object
(
    [success] => 1
    [pagename] => RC1_PGR_Questions
    [result] => stdClass Object
        (
            [CurrentInsuranceStatus] => stdClass Object
                (
                    [QuestionData] => stdClass Object
                        (
                            [_answer] => 
                            [_answerOptions] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [_answerKey] => 340280
                                            [_hideAnswer] => 1
                                            [_order] => 110
                                            [_promptText] => Yes
                                            [_value] => Y
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [1] => stdClass Object
                                        (
                                            [_answerKey] => 340290
                                            [_hideAnswer] => 
                                            [_order] => 120
                                            [_promptText] => Yes, on parent's policy
                                            [_value] => Parents
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [2] => stdClass Object
                                        (
                                            [_answerKey] => 340300
                                            [_hideAnswer] => 
                                            [_order] => 130
                                            [_promptText] => Yes, insured through employer
                                            [_value] => Company
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [3] => stdClass Object
                                        (
                                            [_answerKey] => 340310
                                            [_hideAnswer] => 
                                            [_order] => 140
                                            [_promptText] => No, policy lapsed/expired
                                            [_value] => Lapsed
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [4] => stdClass Object
                                        (
                                            [_answerKey] => 340320
                                            [_hideAnswer] => 
                                            [_order] => 150
                                            [_promptText] => No, never been insured
                                            [_value] => Never
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [5] => stdClass Object
                                        (
                                            [_answerKey] => 340330
                                            [_hideAnswer] => 
                                            [_order] => 160
                                            [_promptText] => No, I didn't have a vehicle to insure
                                            [_value] => First
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [6] => stdClass Object
                                        (
                                            [_answerKey] => 340340
                                            [_hideAnswer] => 
                                            [_order] => 170
                                            [_promptText] => No, been deployed/overseas in the military
                                            [_value] => Military
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                    [7] => stdClass Object
                                        (
                                            [_answerKey] => 340350
                                            [_hideAnswer] => 
                                            [_order] => 180
                                            [_promptText] => No, other reason
                                            [_value] => Other
                                            [_errorLog] => Array
                                                (
                                                )

                                        )

                                )

                            [_answersDivID] => html_CurrentInsuranceStatus
                            [_phoneAreaField] => 
                            [_attributeName] => CurrentInsuranceStatus
                            [_criteriaBasedQuestion] => 1
                            [_dateField] => 
                            [_eventHandler] => 
                            [_fieldType] => select
                            [_hideQuestion] => 1
                            [_monthField] => 
                            [_phoneNumberField] => 
                            [_order] => 10000
                            [_pageAttributes] => stdClass Object
                                (
                                    [additionalLabel] => 
                                    [answerStyle] => 
                                    [colNum] => 
                                    [colWidth] => 
                                    [cssclass] => 
                                    [defaultValue] => 
                                    [isMultiSelect] => 
                                    [maxLength] => 
                                    [min] => 
                                    [questionSkinStyle] => 
                                    [toolTip] => 
                                )

                            [_phonePreFixField] => 
                            [_questionDivID] => prompt_CurrentInsuranceStatus
                            [_questionKey] => 7169010
                            [_required] => 1
                            [_text] => Current insurance status
                            [_yearField] => 
                            [_errorLog] => Array
                                (
                                )

                        )

                )



        )

)

1 回答

  • 0

    所以在我意识到你真正想要做的之后我只使用 serializeunserialize 作为JSON函数的替代品,如下面的工作示例:

    class testobj {
    
        public function __get($name) {
            if(isset($this->{$name}))
                return $this->{$name};
            return false;
        }
    
        public function __set($name,$val) {
            $this->{$name} = $val;
        }
    }
    
    $test = new testobj();
    $test->testvar = "testval";
    
    echo print_r($test,true);
    
    /* Output:
    testobj Object
    (
        [testvar] => testval
    )
    */
    
    $test = serialize($test);
    echo $test;
    
    /* Output:
    O:7:"testobj":1:{s:7:"testvar";s:7:"testval";}testobj Object
    */
    
    $test = unserialize($test);
    
    echo print_r($test,true);
    
    /* Output:
    testobj Object
    (
        [testvar] => testval
    )
    */
    
    echo $test->testvar; // outputs "testval"
    

    我认为 serialize()unserialize() 并不像JSON de- / encoding那么快(我从来没有对此进行测试/基准测试),但它本身存储了对象名称 . 我不知道它是否适用于具有方法的类/对象,但对于上面的数据,它将起作用 . 也有多维结构 .

相关问题