11

json_encode() wont work for me when I'm using åäö. Why? And how can I get it to work?

The php:

echo json_encode($arr);

The javascript:

var theResponse = JSON.parse(xmlHttp.responseText);

When I alert() the response, and the response contains å, ä or ö, the response is = NULL

Please, help me out...

Johan
  • 18,814
  • 30
  • 70
  • 88

8 Answers8

12

It says in the json_encode() documentation:

This function only works with UTF-8 encoded data.

You should convert it to utf-8 with iconv or mbstring first.

Greg
  • 316,276
  • 54
  • 369
  • 333
8

As Greg mentioned, I had to encode åäö to UTF-8. But I did't use iconv or mbstring. When I utf8_encode() all values before putting the values to the array the problem was solved.

Johan
  • 18,814
  • 30
  • 70
  • 88
  • 1
    I'm facing a similar problem, where I've got an associative array which needs to be encoded. I've tried `array_walk_recursive()` with `if(is_string($input)) { return utf_encode($input); } else { return $input }` for the callback but that didn't seem to work. Any ideas? – Anticom Jul 15 '14 at 13:03
  • @Anticom my solution below sorts that out. – Paul Phillips May 26 '15 at 11:14
6

This function will cast the correct data type for the JSON output and utf8_encode the strings.

    /* Change data-type from string to integar or float if required.
     * If string detected then utf8_encode() it. */
    function cast_data_types ($value) {
      if (is_array($value)) {
        $value = array_map('cast_data_types',$value);
        return $value;
      }
      if (is_numeric($value)) {
        if(strpos('.', $value)===false) return (float)$value;
        return (int) $value;
      }
      return utf8_encode((string)$value);
    }

json_encode (cast_data_types($data));
Paul Phillips
  • 1,480
  • 1
  • 15
  • 23
2

JSON defines strings as Unicode!

JSON Definition

You have to encode you ISO to UTF-8

coding Bott
  • 4,287
  • 1
  • 27
  • 44
2

Old question, but figured I'd put this here in case someone needs to log data using json_encode but keep the data untouched, intact for inspection later.

You can encode the data raw using base64_encode, then it will work with json_encode. Later after running json_decode, you can decode the string with base64_decode, you'll get the original data unmodified.

Michael Butler
  • 6,079
  • 3
  • 38
  • 46
1

As of PHP 5.4.0:

Convert your strings in your array to into utf-8 using utf8_encode($str) function.

Then json_encode with JSON_UNESCAPED_UNICODE option:

$arr = json_encode($array, JSON_UNESCAPED_UNICODE);

J. Pichardo
  • 3,077
  • 21
  • 37
George
  • 11
  • 1
0

Using the standard method when reading from MySQL:

$resultArray = array();
while($obj = MySQL_fetch_object($res)) {
 $resultArray[] = $obj;
}
$result = json_encode($resultArray);

The encoding can be done using the following:

$resultArray = array();
while($obj = MySQL_fetch_object($res)) {
 foreach($obj as $key => $value) {
  if (!is_null($value)) {
   $obj->$key = utf8_encode($value);
  }
 }
 $resultArray[] = $obj;
}
$result = json_encode($resultArray);

The if is_null has to be included so that null fields (e.g., DateTime fields) remain null in the output.

TRiG
  • 10,148
  • 7
  • 57
  • 107
BennyBechDk
  • 934
  • 7
  • 13
0

The $data (in my case) is an array with text values as ISO-8859-1. The trick below prepares $data to be used with json_encode.

function toUtf8(&$v, $k) {
    $v = utf8_encode($v);
}
array_walk_recursive($data, 'toUtf8');
Fabio Montefuscolo
  • 2,258
  • 2
  • 21
  • 18