解析 JSON 包含冒号的字符串

Parse JSON string containing a colon

我用来解析 JSON 的 class 不允许您在任何字符串中使用冒号。例如,这将导致解析错误:

{
    "Test:": "Just a test"
}

关于如何使这个 class 忽略字符串中的冒号的任何想法?这是我正在使用的 class:

class JSON {
    static var inst;
    var text;
    function JSON() {
    }
    static function getInstance() {
        if (inst == null) {
            inst = new cinqetdemi.JSON();
        }
        return (inst);
    }
    static function stringify(arg) {
        var _local3;
        var _local2;
        var _local6;
        var _local1 = "";
        var _local4;
        switch (typeof (arg)) {
            case "object" :
                if (arg) {
                    if (arg instanceof Array) {
                        _local2 = 0;
                        while (_local2 < arg.length) {
                            _local4 = stringify(arg[_local2]);
                            if (_local1) {
                                _local1 = _local1 + ",";
                            }
                            _local1 = _local1 + _local4;
                            _local2++;
                        }
                        return (("[" + _local1) + "]");
                    } else if (typeof (arg.toString) != "undefined") {
                        for (_local2 in arg) {
                            _local4 = arg[_local2];
                            if ((typeof (_local4) != "undefined") && (typeof (_local4) != "function")) {
                                _local4 = stringify(_local4);
                                if (_local1) {
                                    _local1 = _local1 + ",";
                                }
                                _local1 = _local1 + ((stringify(_local2) + ":") + _local4);
                            }
                        }
                        return (("{" + _local1) + "}");
                    }
                }
                return ("null");
            case "number" :
                return ((isFinite(arg) ? (String(arg)) : "null"));
            case "string" :
                _local6 = arg.length;
                _local1 = "\"";
                _local2 = 0;
                while (_local2 < _local6) {
                    _local3 = arg.charAt(_local2);
                    if (_local3 >= " ") {
                        if ((_local3 == "\") || (_local3 == "\"")) {
                            _local1 = _local1 + "\";
                        }
                        _local1 = _local1 + _local3;
                    } else {
                        switch (_local3) {
                            case "\b" :
                                _local1 = _local1 + "\b";
                                break;
                            case "\f" :
                                _local1 = _local1 + "\f";
                                break;
                            case newline :
                                _local1 = _local1 + "\n";
                                break;
                            case "\r" :
                                _local1 = _local1 + "\r";
                                break;
                            case "\t" :
                                _local1 = _local1 + "\t";
                                break;
                            default :
                                _local3 = _local3.charCodeAt();
                                _local1 = _local1 + (("\u00" + Math.floor(_local3 / 16).toString(16)) + (_local3 % 16).toString(16));
                        }
                    }
                    _local2 = _local2 + 1;
                }
                return (_local1 + "\"");
            case "boolean" :
                return (String(arg));
        }
        return ("null");
    }
    static function parse(text) {
        if (!text.length) {
            throw new Error("JSONError: Text missing");
        }
        var _local1 = getInstance();
        _local1.at = 0;
        _local1.ch = " ";
        _local1.text = text;
        return (_local1.value());
    }
    function error(m) {
        var _local2 = ((("JSONError: " + m) + " at ") + (at - 1)) + newline;
        _local2 = _local2 + (text.substr(at - 10, 20) + newline);
        _local2 = _local2 + "        ^";
        throw new Error(_local2);
    }
    function next() {
        ch = text.charAt(at);
        at = at + 1;
        return (ch);
    }
    function white() {
        while (ch) {
            if (ch <= " ") {
                next();
            } else if (ch == "/") {
                switch (next()) {
                    case "/" :
                        while ((next() && (ch != newline)) && (ch != "\r")) {
                        }
                        break;
                    case "*" :
                        next();
                        while (true) {
                            if (ch) {
                                if (ch == "*") {
                                    if (next() == "/") {
                                        next();
                                        break;
                                    }
                                } else {
                                    next();
                                }
                            } else {
                                error("Unterminated comment");
                            }
                        }
                        break;
                    default :
                        error("Syntax error");
                }
            } else {
                break;
            }
        }
    }
    function str() {
        var _local5;
        var _local2 = "";
        var _local4;
        var _local3;
        var _local6 = false;
        if ((ch == "\"") || (ch == "'")) {
            var _local7 = ch;
            while (next()) {
                if (ch == _local7) {
                    next();
                    return (_local2);
                } else if (ch == "\") {
                    switch (next()) {
                        case "b" :
                            _local2 = _local2 + "\b";
                            break;
                        case "f" :
                            _local2 = _local2 + "\f";
                            break;
                        case "n" :
                            _local2 = _local2 + newline;
                            break;
                        case "r" :
                            _local2 = _local2 + "\r";
                            break;
                        case "t" :
                            _local2 = _local2 + "\t";
                            break;
                        case "u" :
                            _local3 = 0;
                            _local5 = 0;
                            while (_local5 < 4) {
                                _local4 = parseInt(next(), 16);
                                if (!isFinite(_local4)) {
                                    _local6 = true;
                                    break;
                                }
                                _local3 = (_local3 * 16) + _local4;
                                _local5 = _local5 + 1;
                            }
                            if (_local6) {
                                _local6 = false;
                                break;
                            }
                            _local2 = _local2 + String.fromCharCode(_local3);
                            break;
                        default :
                            _local2 = _local2 + ch;
                    }
                } else {
                    _local2 = _local2 + ch;
                }
            }
        }
        error("Bad string");
    }
    function key() {
        var _local2 = ch;
        var _local6 = false;
        var _local3 = text.indexOf(":", at);
        var _local4 = text.indexOf("\"", at);
        var _local5 = text.indexOf("'", at);
        if (((_local4 <= _local3) && (_local4 > -1)) || ((_local5 <= _local3) && (_local5 > -1))) {
            _local2 = str();
            white();
            if (ch == ":") {
                return (_local2);
            } else {
                error("Bad key");
            }
        }
        while (next()) {
            if (ch == ":") {
                return (_local2);
            }
            if (ch <= " ") {
            } else {
                _local2 = _local2 + ch;
            }
        }
        error("Bad key");
    }
    function arr() {
        var _local2 = [];
        if (ch == "[") {
            next();
            white();
            if (ch == "]") {
                next();
                return (_local2);
            }
            while (ch) {
                if (ch == "]") {
                    next();
                    return (_local2);
                }
                _local2.push(value());
                white();
                if (ch == "]") {
                    next();
                    return (_local2);
                } else if (ch != ",") {
                    break;
                }
                next();
                white();
            }
        }
        error("Bad array");
    }
    function obj() {
        var _local3;
        var _local2 = {};
        if (ch == "{") {
            next();
            white();
            if (ch == "}") {
                next();
                return (_local2);
            }
            while (ch) {
                if (ch == "}") {
                    next();
                    return (_local2);
                }
                _local3 = this.key();
                if (ch != ":") {
                    break;
                }
                next();
                _local2[_local3] = value();
                white();
                if (ch == "}") {
                    next();
                    return (_local2);
                } else if (ch != ",") {
                    break;
                }
                next();
                white();
            }
        }
        error("Bad object");
    }
    function num() {
        var _local2 = "";
        var _local3;
        if (ch == "-") {
            _local2 = "-";
            next();
        }
        while (((((ch >= "0") && (ch <= "9")) || (ch == "x")) || ((ch >= "a") && (ch <= "f"))) || ((ch >= "A") && (ch <= "F"))) {
            _local2 = _local2 + ch;
            next();
        }
        if (ch == ".") {
            _local2 = _local2 + ".";
            next();
            while ((ch >= "0") && (ch <= "9")) {
                _local2 = _local2 + ch;
                next();
            }
        }
        if ((ch == "e") || (ch == "E")) {
            _local2 = _local2 + ch;
            next();
            if ((ch == "-") || (ch == "+")) {
                _local2 = _local2 + ch;
                next();
            }
            while ((ch >= "0") && (ch <= "9")) {
                _local2 = _local2 + ch;
                next();
            }
        }
        _local3 = Number(_local2);
        if (!isFinite(_local3)) {
            error("Bad number");
        }
        return (_local3);
    }
    function word() {
        switch (ch) {
            case "t" :
                if (((next() == "r") && (next() == "u")) && (next() == "e")) {
                    next();
                    return (true);
                }
                break;
            case "f" :
                if ((((next() == "a") && (next() == "l")) && (next() == "s")) && (next() == "e")) {
                    next();
                    return (false);
                }
                break;
            case "n" :
                if (((next() == "u") && (next() == "l")) && (next() == "l")) {
                    next();
                    return (null);
                }
                break;
        }
        error("Syntax error");
    }
    function value() {
        white();
        switch (ch) {
            case "{" :
                return (obj());
            case "[" :
                return (arr());
            case "\"" :
            case "'" :
                return (str());
            case "-" :
                return (num());
        }
        return ((((ch >= "0") && (ch <= "9")) ? (num()) : (word())));
    }
    var at = 0;
    var ch = " ";
}

看看 my answer of this question 并使用我在那里提到的 JSON class,它与您的测试内容配合得很好:

import JSON;

var json = new JSON();
var data:String = '{"Test:": "Just a test"}';

trace(json.parse(data)['Test:']);   // gives : Just a test

希望能帮到你。