&tag(JSON);
PHPでJSON - Do You PHP?よりphp5.3の場合は次のようにすればいいらしい。文字化けしたので最初のheader行を追加した。
<?php header("Content-Type: text/html; charset=UTF-8"); ?>
<?php
echo '<pre>';
$val = array("abc" => 12,
"foo" => "bar",
"bool0" => false,
"bool1" => true,
"arr" => array(1, 2, 3, null, 5),
"float" => 1.2345,
"hoge" => mb_convert_encoding('ほげほげ', 'utf-8', mb_internal_encoding())
);
$output = json_encode($val);
echo 'encoded=' . $output . '<br>';
$decoded = json_decode($output);
var_dump($decoded);
echo mb_convert_encoding($decoded->hoge, mb_internal_encoding(), 'utf-8');
?>
</pre>
<hr>
<?php
show_source($_SERVER['SCRIPT_FILENAME']);
?>
次のように出力される。
encoded={"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345,"hoge":"\u307b\u3052\u307b\u3052"}
object(stdClass)#1 (7) {
["abc"]=>
int(12)
["foo"]=>
string(3) "bar"
["bool0"]=>
bool(false)
["bool1"]=>
bool(true)
["arr"]=>
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
NULL
[4]=>
int(5)
}
["float"]=>
float(1.2345)
["hoge"]=>
string(12) "ほげほげ"
}
ほげほげ