Table of Contents

JSON parser

...based on the one fount at http://www.json.org, but this one is much more like the php-pecl-json package.

json.js

/**
 * JavaScript JSON-helpers, function oriented like the php-pecl-jason package
 * @autor           Alexander 'E-Razor' Krause      <alexander.krause@erazor-zone.de>
 * @date            2006.06.23
 * @version         0.1.1
 * @copyright       GPL-2
 */
 
 
//recursive function to solve subitems
function _json_encode_walk(mixed){
  var retval=new String();
  if(mixed!==undefined) {
    if (mixed instanceof Array) {
      for (var key in mixed) {
        retval+=_json_encode_walk(mixed[key])+",";
      }
      return "["+retval.slice(0,retval.length-1)+"]";
    } else if (typeof mixed =="object") {
      for (var key in mixed) {
        retval+="\""+key+"\":"+_json_encode_walk(mixed[key])+",";
      }
      return "{"+retval.slice(0,retval.length-1)+"}";
    } else if (typeof mixed=="number") {
      if (isFinite(mixed)) {
        return mixed;
      } else {
        return "null";
      }
    } else if (typeof mixed=="string") {
      if (/["\\\x00-\x1f]/.test(mixed)) {
        mixed = mixed.replace(/([\x00-\x1f\\"])/g,
          function(a, b) {
            var m={
              '\b': '\\b',
              '\t': '\\t',
              '\n': '\\n',
              '\f': '\\f',
              '\r': '\\r',
              '"' : '\\"',
              '\\': '\\\\'
            };
            var c = m[b];
            if (c) {
              return c;
            }
            c = b.charCodeAt();
            return '\\u00' +
              Math.floor(c/16).toString(16) +(c%16).toString(16);
          }
        );
      }
      return "\""+mixed+"\"";
    } else if (typeof mixed=="boolean") {
      return mixed?"true":"false";
    }
  } else {
    return "null";
  }
}
 
 
//transform object to string
function json_encode(mixed){
  return _json_encode_walk(mixed);
}
 
//transform string to object
function json_decode(string){
  try {
      return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
              string.replace(/"(\\.|[^"\\])*"/g, ''))) &&
          eval('(' + string + ')');
  } catch (e) {
      return false;
  }
}

json-test.html

<html>
  <head>
  <script src="../lib/js/json.js" type="text/javascript"></script>
  </head>
  <body>
  <pre>
  <script type="text/javascript">
    document.write("\n");
    document.write(json_encode([0,1,2,3,4,5,6,"red","blue","green"])+"\n");
    document.write(json_encode({'key_0':'value_0','bool_0':true,'number':123.12,'Array':[1,2,3,4]})+"\n");
    document.write(json_encode([0,1,2,3,4])+"\n");
    document.write(json_encode(undefined));
  </script>
  </pre>
  </body>
</html>
[0,1,2,3,4,5,6,"red","blue","green"]
{"key_0":"value_0","bool_0":true,"number":123.12,"Array":[1,2,3,4]}
[0,1,2,3,4]
null
 
wiki/projects/js/json.txt · Last modified: 2006/06/23 19:36 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki