TOP ▲
itcore TOP プログラムパーツ
uJsonEncode JSONエンコード 日本語をエスケープしない | itcore 2021年
PHP
関数
<?php
// JSONエンコード 日本語をエスケープしない
function uJsonEncode($a1)
{
  if (!is_array($a1)) {
    return "{}";
  }
  return json_encode($a1, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
}
// JSONデコード 配列にする。
function uJsonDecode($json)
{
  if ("" == $json) {
    return [];
  }
  return json_decode($json, true);
}
?>
テストプログラム
<?php include_once "uJsonEncode.func"; ?>
<?php
$a1 = array("言語" => "日本語");
$json = uJsonEncode($a1);
echo "$json<br>\n";
$a1 = uJsonDecode($json);
print_r($a1);
?>
実行結果
{
"言語": "日本語"
}
Array
(
[言語] => 日本語
)