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

 鱼儿7秒的记忆_710 发布于 2023-02-11 10:36

为什么这个数组:

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

user555.. 6

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

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

请注意,代码使用lambda,需要PHP 5.3.0或更高版本.但是,如果之前定义了匿名函数array_walk_recursive然后作为回调传递,则可以轻松地重构它- 就像这样array_walk_recursive($array, 'nullStrToNull');.

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有