爲什麼會這樣排列:PHP json_encode - 空和INT值作爲字符串返回
Array
(
[EventCode] => 20140709TXAO
[ShortParticipant] => Array
(
[Address1] => null
[Address2] => null
[Address3] => null
[City] => null
[Country] => null
[Email] => [email protected]
[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": "[email protected]",
"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 '[email protected]' (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;
}
[email protected]:d – OptimusCrime
你忘了張貼代碼 – Musa
這不是json_encode'的'默認行爲,如@Musa說,我們展示的代碼 –