gpt4 book ai didi

javascript - Json 编码 数组与仅转换为数组的对象的混合

转载 作者:行者123 更新时间:2023-11-28 06:10:27 24 4
gpt4 key购买 nike

我的 php 函数返回一个数组,我使用

将其转换为 json

$req = json_encode($questionData);

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

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

我想获得包含 arrya 和对象数据混合的相同数组。

下面是请求数组

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
(
)

)

)



)

)

最佳答案

所以在我意识到你真正想做的事情之后,我只是使用 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 解码/编码那么快(我从未对此进行过测试/基准测试),但它存储对象名称本身。我不知道它是否适用于具有方法的类/对象,但对于上面的数据它会起作用。也具有多维结构。

关于javascript - Json 编码 数组与仅转换为数组的对象的混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36446773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com