1
0

phpでJSONを生成してjsに渡す時の注意点

Posted at

jsonを生成する時に単純に

$json = json_encode($str);

としていないだろうか。単なる数値だけならこれでよいが、例えば、

$str[1] = "G'men '75";
$str[2] = '\2,800';
$str[3] = 'my name is "john"';

というような文字列をエンコードしてjavascriptに渡すとエラーになる。

$json = json_encode($str, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS );
$json = str_replace('\\', '\\\\', $json);

とするのがよい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0