盗用石头君的代码<script type="text/javascript">var JSON = function(sJSON){
this.objType = (typeof sJSON);
this.self = [];
(function(s,o){for(var i in o){o.hasOwnProperty(i)&&(s[i]=o[i],s.self[i]=o[i])};})(this,(this.objType=='string')?eval('0,'+sJSON):sJSON);
}
JSON.prototype = {
toString:function(){
return this.getString();
},
valueOf:function(){
return this.getString();
},
getString:function(){
var sA = [];
(function(o){
var oo = null;
sA.push('{');
for(var i in o){
if(o.hasOwnProperty(i) && i!='prototype'){
oo = o[i];
if(oo instanceof Array){
sA.push(i+':[');
for(var b in oo){
if(oo.hasOwnProperty(b) && b!='prototype'){
sA.push(oo[b]+',');
if(typeof oo[b]=='object') arguments.callee(oo[b]);
}
}
sA.push('],');
continue;
}else{
sA.push(i+':'+oo+',');
}
if(typeof oo=='object') arguments.callee(oo);
}
}
sA.push('},');
})(this.self);
return sA.slice(0).join('').replace(/\[object object\],/ig,'').replace(/,\}/g,'}').replace(/,\]/g,']').slice(0,-1);
},
push:function(sName,sValue){
this.self[sName] = sValue;
this[sName] = sValue;
}
}var strA = '{allPrivList: [{id:"func001", desc:"商品管理"},{id:"func002", desc:"類別管理"},{id:"func003", desc:"行銷管理"}],groupList: [{id:"g001", desc:"客服", privList: ["func001", "func002", "func004"] },{id:"g002", desc:"營運", privList: ["func003"]}]}';
var objA = new JSON(strA);
alert(objA);
alert(objA.allPrivList[0].desc+'\n'+objA.groupList[0].privList+'\n'+objA.groupList[1].desc);
objA.push("C",[1,"hello baby",function(){alert(this.C[1])}]);
alert(objA);
objA.C[2].call(objA);var objB = new JSON({});
alert(objB);
objB.push("A",function(){alert('test A')});
alert(objB);
objB.A();var objC = new JSON([1]);
alert(objC);
objC.push("A",function(){alert('test A')});
alert(objC.A);var xTree = new JSON({name:'root',list:[{name:'1',list:[{name:'1-1',list:[]},{name:'1-2',list:[]}]},{name:'look at me',list:[]}]});
alert(xTree);var r = xTree.list.splice(1,1)[0];
alert(xTree);xTree.list[0].list.splice(1,0,r);
alert(xTree);</script>
this.objType = (typeof sJSON);
this.self = [];
(function(s,o){for(var i in o){o.hasOwnProperty(i)&&(s[i]=o[i],s.self[i]=o[i])};})(this,(this.objType=='string')?eval('0,'+sJSON):sJSON);
}
JSON.prototype = {
toString:function(){
return this.getString();
},
valueOf:function(){
return this.getString();
},
getString:function(){
var sA = [];
(function(o){
var oo = null;
sA.push('{');
for(var i in o){
if(o.hasOwnProperty(i) && i!='prototype'){
oo = o[i];
if(oo instanceof Array){
sA.push(i+':[');
for(var b in oo){
if(oo.hasOwnProperty(b) && b!='prototype'){
sA.push(oo[b]+',');
if(typeof oo[b]=='object') arguments.callee(oo[b]);
}
}
sA.push('],');
continue;
}else{
sA.push(i+':'+oo+',');
}
if(typeof oo=='object') arguments.callee(oo);
}
}
sA.push('},');
})(this.self);
return sA.slice(0).join('').replace(/\[object object\],/ig,'').replace(/,\}/g,'}').replace(/,\]/g,']').slice(0,-1);
},
push:function(sName,sValue){
this.self[sName] = sValue;
this[sName] = sValue;
}
}var strA = '{allPrivList: [{id:"func001", desc:"商品管理"},{id:"func002", desc:"類別管理"},{id:"func003", desc:"行銷管理"}],groupList: [{id:"g001", desc:"客服", privList: ["func001", "func002", "func004"] },{id:"g002", desc:"營運", privList: ["func003"]}]}';
var objA = new JSON(strA);
alert(objA);
alert(objA.allPrivList[0].desc+'\n'+objA.groupList[0].privList+'\n'+objA.groupList[1].desc);
objA.push("C",[1,"hello baby",function(){alert(this.C[1])}]);
alert(objA);
objA.C[2].call(objA);var objB = new JSON({});
alert(objB);
objB.push("A",function(){alert('test A')});
alert(objB);
objB.A();var objC = new JSON([1]);
alert(objC);
objC.push("A",function(){alert('test A')});
alert(objC.A);var xTree = new JSON({name:'root',list:[{name:'1',list:[{name:'1-1',list:[]},{name:'1-2',list:[]}]},{name:'look at me',list:[]}]});
alert(xTree);var r = xTree.list.splice(1,1)[0];
alert(xTree);xTree.list[0].list.splice(1,0,r);
alert(xTree);</script>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货