用js生成json,其中PostForm需要jQuery,这个项目暂时不用,以后谈研究js编码json
function PostForm(o){
var myf = $(o.form);
var myin = myf.find("input");
var j = 0;
var myin2 = new Array();
for(var i=0;i < myin.length;i ++){
j++;
myin2.push([$(myin[i]).attr('name'),$(myin[i]).val()]);
}
return false;
}
function json_encode_js(aaa){
function je(str){
var a=[],i=0;
var pcs="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (;i<str.length;i++){
if(pcs.indexOf(str[i]) == -1)
a[i]="\\u"+("0000"+str.charCodeAt(i).toString(16)).slice(-4);
else
a[i]=str[i];
}
return a.join("");
}
var i,s,a,aa=[];
if(typeof(aaa)!="object") {alert("ERROR json");return;}
for(i in aaa){
s=aaa[i];
a='"'+je(i)+'":';
if(typeof(s)=='object'){
a+=json_encode_js(s);
}else{
if(typeof(s)=='string')
a+='"'+je(s)+'"';
else if(typeof(s)=='number')
a+=s;
}
aa[aa.length]=a;
}
return "{"+aa.join(",")+"}";
}