トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

PHPでJSONを使う

http://itpro.nikkeibp.co.jp/article/COLUMN/20070307/264116/

基本的にUTF-8で処理することを前提としているので、
その他の文字コードを利用する場合は

mb_convert_variables('UTF-8', 'SHIFT-JIS', $arr);

のように文字コードを変更する

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
  ini_set('error_reporting', E_ALL);
  ini_set('display_errors', '1');
  
  require_once "JSON.php"; 
  $arr = array(
    array(
      "name" => "アシアル",
      "url"  => "http://www.asial.co.jp/"
      ),
    array(
      "name" => "PHPプロ",
      "url"  => "http://www.phppro.jp/"
      )
    );
    
    // $arr["test"] = "あああ";
  
  $json = new Services_JSON;
  mb_convert_variables('UTF-8', 'SHIFT-JIS', $arr);
  $encode = $json->encode($arr);
  
  // header("Content-Type: text/html; charset=shift-jis");
  // echo $encode; 
  
?>

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
    <!--<meta http-equiv="Content-Type" content="text/html; charset=utf-8">-->
    <title>テスト</title>
    
    <script language="javascript">
      function test(){
        var get_json = eval( <?php echo( $encode) ?> );
        // alert(get_json[0]["name"]);
        alert(get_json["test"]);
      }
    </script>
  </head>
  
  <body>
    テスト
    <input type="button" value="test" onClick="javascript:test();">
    
  </body>
</html> 
[カテゴリ: プログラミング言語 > PHP]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2013年02月04日 20時44分55秒