eval() 的替代方法
我一直在使用 eval,它可以在一个网络浏览器中运行,但不能在另一个网络浏览器中运行(这些不是您的标准浏览器,而是与另一个应用程序集成的浏览器)。在其中一个对象中,我的 300 多个项目得到了正确处理。在另一个中,它处理从 256 到末尾的项目,但跳过前 255! JSON 也是正确的。 eval 还有其他选择吗?也许是一个 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;
}
谢谢
I have been using eval which works in one webbrowser but not another (These are not your standard browsers but ones integrated with another application). In one my object of 300+ items is processed correctly. In the opther it processes items from 256 to the end but skips the first 255! the JSON is correct as well. Any alternatives to eval? perhaps a jsonToObject function. I am writing my own at the moment but I am hitting major time constraints and was hoping I didn't have to reinvent the wheel
Any ideas or functions?
Verymuch appreciated
So far I have this, I was going to try and base it on reverse polish. Not sure it's the most efficient, but for some reason it seemed like an interesting challenge. Part of me is folding to the pressures of the time I have though and so I need to be realistic and ask if therre are any methods already out there that can be shared with me.
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;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https://github.com/douglascrockford/JSON-js
https://github.com/douglascrockford/JSON-js