2013-12-12 118 views
1

爲什麼會這樣排列: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; 
} 
+3

[email protected]:d – OptimusCrime

+0

你忘了張貼代碼 – Musa

+0

這不是json_encode'的'默認行爲,如@Musa說,我們展示的代碼 –

回答

6

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

0

轉換爲UTF-8首先,例如

$array = array_map('utf8_encode', array('one', 'two', null, 'three')); 
var_dump(json_encode($array)); 

你會碰到與真/假值同樣的問題。

+0

嵌套數組,不起作用。 – MB34

+0

這難以置信地解決了我的問題!的xD –