json_encode is returning NULL?
I bet you are retrieving data in non-utf8 encoding: try to put mysql_query('SET CHARACTER SET utf8')
before your SELECT
query.
php json_encode returning null
From the manual:
Note that if you try to encode an
array containing non-utf values,
you'll get null values in the
resulting JSON string. You can
batch-encode all the elements of an
array with the array_map function:
$encodedArray = array_map(utf8_encode, $myArr);
echo json_encode($encodedArray);
PHP json_encode is returning null
There are two problems, as far as I can see:
Issue A: Broken JSON in database
Output 1:
{ “status” : 3, “game” : 0, “ships” : { "1" : { ... etc
Those characters “” are not legal in JSON... so you won't be able to parse the data you have within your database as JSON. You will have to replace them with legitimate " characters. Where did the JSON come from?
Issue B: Mixed string & structure
You're mixing JSON-as-a-string (coming from the database) and an array data-structure in PHP (the array of rows from the database) that you wish to represent as JSON.
So to fix that should be doing something like:
<?php
// Convert an array of JSON-Strings to unified array of structured data..
foreach ( $FleetRaw as $key => $sJSONString ){
$FleetRaw[$key] = json_decode($sJSONString);
}
// Now return the whole lot as a json-string to the client
header("Content-type: application/json"); // My assumption of your model..
print json_encode($FleetRaw);
?>
What this should output is an array of objects:
[{ "status" : 3, "game" : 0, "etc" : "..." },{ "status" : 99, "game" : 123, "etc" : "..." },{ "status" : 345, "game" : 456, "etc" : "..." },{ .. }]
Note on your 'nulls' & UTF8 (Output 3)
I would imagine your nulls are caused by PHP failing to even encode the JSON-strings as strings because they contain UTF8 characters -- which is why the Output 3 shows nulls. But those encoding issues may just be those dodgy “” you have in your database.
If you fix issue A you may find you fix Output 3 too. Though this doesn't preclude you from having to address issue B. Output 3 would become an array of your JSON-strings (represented as strings that just happen to look like JSON). Issue B will then sort you out.
Incidentally: http://php.net/manual/en/function.json-last-error.php should help you narrow down any remaining issues with your source JSON if the above doesn't.
Hope this helps! J.
php json_encode() show's null instead of text
json_encode
expects strings in the data to be encoded as UTF-8.
Convert them to UTF-8 if they aren't already:
$results = array_map(function($r) {
$r['text'] = utf8_encode($r['text']);
return $r;
}, $results);
echo json_encode($results);
Related Topics
Remove Non-Utf8 Characters from String
Convert Seconds into Days, Hours, Minutes and Seconds
Slowness Found When Base 64 Image Select and Encode from Database
How to [Recursively] Zip a Directory in PHP
Create a CSV File For a User in PHP
What Does a \ (Backslash) Do in PHP (5.3+)
Suppress Error With @ Operator in PHP
MySQL - This Version of MySQL Doesn't Yet Support 'Limit & In/All/Any/Some Subquery
Fatal Error: Call to a Member Function Fetch_Assoc() on a Non-Object
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate