2

Why would this array:

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
        )

)

be converted to the JSON below with json_encode? Note the null values are being converted to "null"!!! That is causing issues with my receiver.

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

Even the int value is being converted to a string, "1180133".

var_dump results:

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 code:

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 Plugin code:

 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;
 }
7
  • 3
    [email protected] :D Commented Dec 12, 2013 at 21:45
  • You forgot to post the code Commented Dec 12, 2013 at 21:45
  • This is not the default behavior of json_encode, as @Musa said, show us the code Commented Dec 12, 2013 at 21:46
  • 5
    And show the var_dump() of that array rather than the print_r() so we can see types and lengths. Commented Dec 12, 2013 at 21:47
  • 1
    Those are just strings with the word null, not NULL values. Commented Dec 12, 2013 at 22:04

2 Answers 2

7

Before you encode your data with JSON you can do an recursive array walk like this:

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

Note the code uses lambdas and requires PHP 5.3.0 or higher. It can however easily be refactored if the anonymous function is defined before array_walk_recursive and then passed as callback — like so array_walk_recursive($array, 'nullStrToNull');.

As for the integers being casted as strings, the json_encode() option JSON_NUMERIC_CHECK (available since PHP 5.3.3) will encode numeric strings as numbers. Should the option not be available we can use array_walk_recursive() also.

Sign up to request clarification or add additional context in comments.

Comments

0

Convert to UTF-8 first, e.g.

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

You'll run into the same problem with true/false values.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.