PHP json_encode - null& int值作为字符串返回

时间:2013-12-12 21:43:15

标签: php json

为什么这个数组:

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?请注意,空值正在转换为“null”!这导致我的接收器出现问题。

{
    "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;
 }

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

首先转换为UTF-8,例如

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

你会遇到与true / false值相同的问题。