12

I pass php array like

[sit_latitude] => 20° 23.298' N

inside json_encode(); and output is like,

  {"sit_latitude":null}

it's display null, how to resolve this?

my code snippet

...
$stmt->bindParam("id", $_GET[id]);
$stmt->execute();
$employee = $stmt->fetchObject();
$dbh = null;
echo '{"item":'. json_encode($employee) .'}';
Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41

4 Answers4

11

what i tried and succeed was bit strange but worked , so let me share it with you.

$latitude = json_encode(utf8_encode($emplyoee['sit_latitude']));
$longitude = json_encode(utf8_encode($employee['sit_longitude']));

   $emplyoee['sit_latitude'] = $latitude;
    $emplyoee['sit_longitude'] = $longitude;

and now pass like

echo '{"item":'. json_encode($employee) .'}';

this will solve your problem

Shwet
  • 1,848
  • 1
  • 24
  • 35
5

json_encode assumes that strings in its input are valid UTF-8 (keep in mind that PHP strings are sequences of bytes, not sequences of Unicode code points). If your text editor is not set to use that encoding (and you haven't manually generated the UTF-8 using hex escape codes such as \xc2\xb0 in a double-quoted string), the string will be encoded as null (because it is not valid UTF-8).

Other common charsets such as Windows-1252 are also a superset of ASCII, so this is only a problem if there are any non-ASCII characters (U+00B0 DEGREE SIGN included).

Even if the string is valid UTF-8, by default, json_encode will output \u00b0 instead of the actual degree sign. This is not a problem for programmatic decoding using PHP json_decode or JavaScript JSON.parse, which understand JSON hex escape sequences.

(In PHP 5.4, there is a JSON_UNESCAPED_UNICODE flag to prevent this escaping. However, many web servers still have PHP 5.3 or older, and the flag unescapes even the problematic characters U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR. It is possible to simulate the flag in two lines of code, although as I explained above, you don't have to anyway.)

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
4

JSON and escaping characters - this post may help you.

Just pass degree as \u00f8C and then decode it with js.

Community
  • 1
  • 1
Sergei Gorjunov
  • 1,789
  • 1
  • 14
  • 22
-4

You could try htmlspecialchars() function http://php.net/manual/en/function.htmlspecialchars.php

Tudor Ravoiu
  • 2,130
  • 8
  • 35
  • 56