eval()的替代方案

时间:2011-05-19 15:04:55

标签: javascript json object eval

我一直在使用eval,它在一个webbrowser中工作,但不能在另一个webbrowser中工作(这些不是您的标准浏览器,而是与其他应用程序集成的浏览器)。在一个我的300多个项目的对象被正确处理。在opther中它处理从256到最后的项目,但跳过前255个! JSON也是正确的。评估的任何替代方案?也许是一个jsonToObject函数。我现在正在写自己的,但是我遇到了主要的时间限制,希望我不必重新发明轮子

任何想法或功能?

非常感谢

到目前为止,我有这个,我打算尝试以反向抛光为基础。不确定它是最有效的,但由于某种原因,它似乎是一个有趣的挑战。我的一部分是折叠到我所拥有的压力,所以我需要现实,并询问是否有任何可以与我分享的方法。

var operatorStack = new Array();
var variableStack = new Array();
var valueStack = new Array();
var objectStack = new Array();

object.prototype.toJSON = function(){

}

function json(str){
    this.value = str
}

/*
    ({"0":"Zero","1":{"A":"1","B":"2"},"2":{"C":"3","D":"4"},"3":{"E":"5","F":"^"}})
*/

json.prototype.toObject = function(){
    var str = this.value;
    var vbl = "";
    var vlu = "";
    for(var i = 0; i < str.length; i++){
        var chr = str.charAt(i);
        switch(chr){
            case '{': //new Object();
                objectStack.push = new Object();
                operatorStack.push(chr);
                break;
            case '}': //close Object();
                operatorStack.pop();
                if(operatorStack[operatorStack.length-1] == ':'){
                    objectStack[objectStack.length-2][vbl] = objectStack[objectStack.length-1];
                }else{
                    return obj;
                }
                break;
            case '"':
                if(operatorStack[operatorStack.length-1] == '"'){
                    operatorStack.pop();
                    if(operatorStack[operatorStack.length-1] != ':'){
                        objectStack[objectStack.length-1][vbl] = val;
                        vbl = "";
                        val = "";
                        operatorStack.pop();
                    }else if(operatorStack[operatorStack.length-1] != ','){
                        operatorStack.pop();
                    }
                }else{
                    operatorStack.push(chr);
                }
                break;
            case ':':
                operatorStack.push(chr);
                break;
            case ',':
                operatorStack.push(chr);
                break;
            default:
                if(operatorStack[operatorStack.length-1] == '"'){
                    switch(operatorStack[operatorStack.length-2]{
                        case '{': //we know it's a variable
                            vbl += chr;
                            break;
                        case ',': //we know it's a variable
                            vbl += chr;
                            break;
                        case ':': //we know it's a value
                            val += chr;
                            break;
                    }
                }else{
                    alert("JSON not formed properly, alphanumerics not within quotes.")//JSON not formed properly
                }
                break;
        }
    }
    return obj;
}

由于

2 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)