Prototype-string-evaljson

提供:Dev Guides
移動先:案内検索

プロトタイプ-evalJSON()メソッド

このメソッドは、文字列内のJSONを評価し、結果のオブジェクトを返します。 オプションのsanitizeパラメーターがtrueに設定されている場合、文字列は悪意のある試みの可能性がないかチェックされ、検出された場合はevalは呼び出されません。

構文

string.evalJSON([sanitize = false]);

戻り値

文字列を返します。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            var str = '{ "name": "Violet", "occupation": "character" }';
            var person = str.evalJSON();
            alert( "Name :" + person.name);
            alert( "Occupation :" + person.occupation);
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br/>
      <br/>
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

出力