actionscript 2 - Problem using JSON.as -
my code
import json; var j = "{fef:34}"; var json = new json(); trace(json.parse(j)); // undefined !!!
it doesn't work; trace return undefined. i'm compiling flash 8 in actionscript 2. file json.as
in same folder (see below).
when debugging says value json
after json = new json()
undefined.
json.as (from json.org)
class json { var ch:string = ''; var at:number = 0; var t,u; var text:string; function stringify(arg):string { var c, i, l, s = '', v; switch (typeof arg) { case 'object': if (arg) { if (arg instanceof array) { (i = 0; < arg.length; ++i) { v = stringify(arg[i]); if (s) { s += ','; } s += v; } return '[' + s + ']'; } else if (typeof arg.tostring != 'undefined') { (i in arg) { v = arg[i]; if (typeof v != 'undefined' && typeof v != 'function') { v = stringify(v); if (s) { s += ','; } s += stringify(i) + ':' + v; } } return '{' + s + '}'; } } return 'null'; case 'number': return isfinite(arg) ? string(arg) : 'null'; case 'string': l = arg.length; s = '"'; (i = 0; < l; += 1) { c = arg.charat(i); if (c >= ' ') { if (c == '\\' || c == '"') { s += '\\'; } s += c; } else { switch (c) { case '\b': s += '\\b'; break; case '\f': s += '\\f'; break; case '\n': s += '\\n'; break; case '\r': s += '\\r'; break; case '\t': s += '\\t'; break; default: c = c.charcodeat(); s += '\\u00' + math.floor(c / 16).tostring(16) + (c % 16).tostring(16); } } } return s + '"'; case 'boolean': return string(arg); default: return 'null'; } } function white() { while (ch) { if (ch <= ' ') { this.next(); } else if (ch == '/') { switch (this.next()) { case '/': while (this.next() && ch != '\n' && ch != '\r') {} break; case '*': this.next(); (;;) { if (ch) { if (ch == '*') { if (this.next() == '/') { next(); break; } } else { this.next(); } } else { error("unterminated comment"); } } break; default: this.error("syntax error"); } } else { break; } } } function error(m) { throw { name: 'jsonerror', message: m, at: @ - 1, text: text }; } function next() { ch = text.charat(at); @ += 1; return ch; } function str() { var i, s = '', t, u; var outer:boolean = false; if (ch == '"') { while (this.next()) { if (ch == '"') { this.next(); return s; } else if (ch == '\\') { switch (this.next()) { case 'b': s += '\b'; break; case 'f': s += '\f'; break; case 'n': s += '\n'; break; case 'r': s += '\r'; break; case 't': s += '\t'; break; case 'u': u = 0; (i = 0; < 4; += 1) { t = parseint(this.next(), 16); if (!isfinite(t)) { outer = true; break; } u = u * 16 + t; } if(outer) { outer = false; break; } s += string.fromcharcode(u); break; default: s += ch; } } else { s += ch; } } } this.error("bad string"); } function arr() { var = []; if (ch == '[') { this.next(); this.white(); if (ch == ']') { this.next(); return a; } while (ch) { a.push(this.value()); this.white(); if (ch == ']') { this.next(); return a; } else if (ch != ',') { break; } this.next(); this.white(); } } this.error("bad array"); } function obj() { var k, o = {}; if (ch == '{') { this.next(); this.white(); if (ch == '}') { this.next(); return o; } while (ch) { k = this.str(); this.white(); if (ch != ':') { break; } this.next(); o[k] = this.value(); this.white(); if (ch == '}') { this.next(); return o; } else if (ch != ',') { break; } this.next(); this.white(); } } this.error("bad object"); } function num() { var n = '', v; if (ch == '-') { n = '-'; this.next(); } while (ch >= '0' && ch <= '9') { n += ch; this.next(); } if (ch == '.') { n += '.'; this.next(); while (ch >= '0' && ch <= '9') { n += ch; this.next(); } } if (ch == 'e' || ch == 'e') { n += ch; this.next(); if (ch == '-' || ch == '+') { n += ch; this.next(); } while (ch >= '0' && ch <= '9') { n += ch; this.next(); } } v = number(n); if (!isfinite(v)) { this.error("bad number"); } return v; } function word() { switch (ch) { case 't': if (this.next() == 'r' && this.next() == 'u' && this.next() == 'e') { this.next(); return true; } break; case 'f': if (this.next() == 'a' && this.next() == 'l' && this.next() == 's' && this.next() == 'e') { this.next(); return false; } break; case 'n': if (this.next() == 'u' && this.next() == 'l' && this.next() == 'l') { this.next(); return null; } break; } this.error("syntax error"); } function value() { this.white(); switch (ch) { case '{': return this.obj(); case '[': return this.arr(); case '"': return this.str(); case '-': return this.num(); default: return ch >= '0' && ch <= '9' ? this.num() : this.word(); } } function parse(_text:string):object { text = _text; @ = 0; ch = ' '; return value(); } }
why json
undefined @ end of code?
make sure file name json.as upper case.
if write import json;
, file name json.as undefined!
> blockquote import json; var json = new json(); trace(json); // output: [object object] enter code here
Comments
Post a Comment