很拽的JSON
JSON,超拽,运行一下这个代码就知道了:
<div id="mydiv">
<table border="{border}">
<tr><th>Last</th><td>{last}</td></tr>
<tr><th>First</th><td>{first}</td></tr>
</table>
</div>
<br>
数据列表---
<div id="ruleDiv">
<tr><td>{$.name}</td><td>{$.age}</td></tr>
</div>
<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ?
r : a;
}
);
};
function jsonT(self, rules) {
var T = {
output: false,
init: function() {
for (var rule in rules)
if (rule.substr(0,4) != "self")
rules["self."+rule] = rules[rule];
return this;
},
apply: function(expr) {
var trf = function(s){ return s.replace(/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/g,
function($0,$1){return T.processArg($1, expr);})},
x = expr.replace(/\[[0-9]+\]/g, "[*]"), res;
if (x in rules) {
if (typeof(rules[x]) == "string")
res = trf(rules[x]);
else if (typeof(rules[x]) == "function")
res = trf(rules[x](eval(expr)).toString());
}
else
res = T.eval(expr);
return res;
},
processArg: function(arg, parentExpr) {
var expand = function(a,e){return (e=a.replace(/^\$/,e)).substr(0,4)!="self" ? ("self."+e) : e; },
res = "";
T.output = true;
if (arg.charAt(0) == "@")
res = eval(arg.replace(/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/,
function($0,$1,$2){return "rules['self."+$1+"']("+expand($2,parentExpr)+")";}));
else if (arg != "$")
res = T.apply(expand(arg, parentExpr));
else
res = T.eval(parentExpr);
T.output = false;
return res;
},
eval: function(expr) {
var v = eval(expr), res = "";
if (typeof(v) != "undefined") {
if (v instanceof Array) {
for (var i=0; i<v.length; i++)
if (typeof(v[i]) != "undefined")
res += T.apply(expr+"["+i+"]");
}
else if (typeof(v) == "object") {
for (var m in v)
if (typeof(v[m]) != "undefined")
res += T.apply(expr+"."+m);
}
else if (T.output)
res += v;
}
return res;
}
};
return T.init().apply("self");
}
var data = {
"first": "Carl",
"last": "Hollywood",
"border": 2
};
mydiv.innerHTML = mydiv.innerHTML.supplant(data);
//test JSONT
var arrayData =
[
{name:"tommy",age:30},
{name:"angel",age:28},
{name:"lina",age:22}
];
var rule = {"self": "<table border=1>\n{$}</table>\n",
"self[*]": ruleDiv.innerHTML};
ruleDiv.innerHTML = jsonT(arrayData,rule);
//-->
</SCRIPT>
<table border="{border}">
<tr><th>Last</th><td>{last}</td></tr>
<tr><th>First</th><td>{first}</td></tr>
</table>
</div>
<br>
数据列表---
<div id="ruleDiv">
<tr><td>{$.name}</td><td>{$.age}</td></tr>
</div>
<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ?
r : a;
}
);
};
function jsonT(self, rules) {
var T = {
output: false,
init: function() {
for (var rule in rules)
if (rule.substr(0,4) != "self")
rules["self."+rule] = rules[rule];
return this;
},
apply: function(expr) {
var trf = function(s){ return s.replace(/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/g,
function($0,$1){return T.processArg($1, expr);})},
x = expr.replace(/\[[0-9]+\]/g, "[*]"), res;
if (x in rules) {
if (typeof(rules[x]) == "string")
res = trf(rules[x]);
else if (typeof(rules[x]) == "function")
res = trf(rules[x](eval(expr)).toString());
}
else
res = T.eval(expr);
return res;
},
processArg: function(arg, parentExpr) {
var expand = function(a,e){return (e=a.replace(/^\$/,e)).substr(0,4)!="self" ? ("self."+e) : e; },
res = "";
T.output = true;
if (arg.charAt(0) == "@")
res = eval(arg.replace(/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/,
function($0,$1,$2){return "rules['self."+$1+"']("+expand($2,parentExpr)+")";}));
else if (arg != "$")
res = T.apply(expand(arg, parentExpr));
else
res = T.eval(parentExpr);
T.output = false;
return res;
},
eval: function(expr) {
var v = eval(expr), res = "";
if (typeof(v) != "undefined") {
if (v instanceof Array) {
for (var i=0; i<v.length; i++)
if (typeof(v[i]) != "undefined")
res += T.apply(expr+"["+i+"]");
}
else if (typeof(v) == "object") {
for (var m in v)
if (typeof(v[m]) != "undefined")
res += T.apply(expr+"."+m);
}
else if (T.output)
res += v;
}
return res;
}
};
return T.init().apply("self");
}
var data = {
"first": "Carl",
"last": "Hollywood",
"border": 2
};
mydiv.innerHTML = mydiv.innerHTML.supplant(data);
//test JSONT
var arrayData =
[
{name:"tommy",age:30},
{name:"angel",age:28},
{name:"lina",age:22}
];
var rule = {"self": "<table border=1>\n{$}</table>\n",
"self[*]": ruleDiv.innerHTML};
ruleDiv.innerHTML = jsonT(arrayData,rule);
//-->
</SCRIPT>