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) .'}';
The json_encode() function is used to encode a value to JSON format.
Syntax. The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.
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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With