gpt4 book ai didi

PHP json_encode - 以字符串形式返回的 null 和 int 值

转载 作者:行者123 更新时间:2023-12-03 22:54:52 27 4
gpt4 key购买 nike

为什么这个数组:

Array
(
[EventCode] => 20140709TXAO
[ShortParticipant] => Array
(
[Address1] => null
[Address2] => null
[Address3] => null
[City] => null
[Country] => null
[Email] => kkardashian@kkardashain.com
[Employer] => TNA
[FirstName] => Kim
[LastName] => Kardashian
[PID] => 1180133
[Result] => null
[State] => null
)

)

用json_encode转成下面的JSON?请注意,空值正在转换为“空”!!!这导致我的接收器出现问题。

{
"EventCode": "20140709TXAO",
"ShortParticipant": {
"Address1": "null",
"Address2": "null",
"Address3": "null",
"City": "null",
"Country": "null",
"Email": "kkardashian@kkardashain.com",
"Employer": "TNA",
"FirstName": "Kim",
"LastName": "Kardashian",
"PID": "1180133",
"State": "null"
}
}

甚至 int 值也被转换为字符串“1180133”。

var_dump 结果:

array
'EventCode' => string '20140709TXAO' (length=12)
'ShortParticipant' =>
array
'Address1' => string 'null' (length=4)
'Address2' => string 'null' (length=4)
'Address3' => string 'null' (length=4)
'City' => string 'null' (length=4)
'Country' => string 'null' (length=4)
'Email' => string 'kkardashian@kkardashain.com' (length=27)
'Employer' => string 'TNA' (length=3)
'FirstName' => string 'Kim' (length=3)
'LastName' => string 'Kardashian' (length=10)
'PID' => string '1180133' (length=7)
'Result' => string 'null' (length=4)
'State' => string 'null' (length=4)

Javascript 代码:

function callRegStatus(eventcode, RegStatus) {
inputdata = {LogonTicket: logonticket,
data2: $.extend(true, {EventCode: eventcode},
{ShortParticipant: RegStatus})};
ok_to_proceed = false;
$.ajax({async: false
, type:'POST'
, url: REGFUNCTIONS_URL
, data: inputdata
, dataType: 'json'
, success: function(data) {
ok_to_proceed = true;
}
, error: function(jqXHR, textStatus, errorThrown) {
ok_to_proceed = false;
$("#error_message").html(jqXHR.responseText);
}
});
return ok_to_proceed;
}

EE插件代码:

 public function getRegStatus() {
$data = $_POST;
$data2 = $data["data2"];

$url = $this->server . '/RegStatus/'.$data["LogonTicket"];
$options = array(CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data2)
);
$ch = curl_init();
curl_setopt_array($ch, $options);

$RegStatusResult = curl_exec($ch);

curl_close($ch);
return $RegStatusResult;
}

最佳答案

在使用 JSON 对数据进行编码之前,您可以像这样执行递归数组遍历:

array_walk_recursive($array, function(&$item, $key) {
if ($item == 'null') $item = NULL;
});

注意代码使用了lambdas并且需要 PHP 5.3.0 或更高版本。但是,如果在 array_walk_recursive 之前定义了匿名函数,然后将其作为回调传递,那么它可以很容易地被重构——就像这样 array_walk_recursive($array, 'nullStrToNull');

对于转换为字符串的整数,json_encode() 选项 JSON_NUMERIC_CHECK(自 PHP 5.3.3 起可用)会将数字字符串编码为数字。如果该选项不可用,我们也可以使用 array_walk_recursive()

关于PHP json_encode - 以字符串形式返回的 null 和 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554876/

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