Simple implementation of Javascript json object and string conversion
function obj2str(o){ var r = []; if(typeof o == "string" || o == null) { return o; } if(typeof o == "object"){ if(!){ r[0]="{" for(var i in o){ r[]=i; r[]=":"; r[]=obj2str(o[i]); r[]=","; } r[-1]="}" }else{ r[0]="[" for(var i =0;i<;i++){ r[]=obj2str(o[i]); r[]=","; } r[-1]="]" } return (""); } return (); }
string to object
function taoRan(){ var str='{"result": [["Cash Withdrawal", "219"], ["Cash withdrawal bank", "121"], ["Refund", "272"], ["Cash withdrawal failed", "16"], ["Group Buy", "15"], ["Taobao", "412"],["*", "58"], ["Cash withdrawal limit", "16"], ["Cash withdrawal time", "81"], ["* Certification", "26"]]}'; alert(strToObj(str).result[2]); } function strToObj(json){ return eval("("+json+")"); }
Very useful JsonToString method
//' JsontostringCode function JsonToString(o) { var arr = []; var fmt = function(s) { if (typeof s == 'object' && s != null) return JsonToStr(s); return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s; } for (var i in o) ("'" + i + "':" + fmt(o[i])); return '{' + (',') + '}'; }
I've used it very well anyway, haha
In addition: If you don't want the numbers in json to be stringified, you can transform: return /^(string|number)$/.test(typeof s) ? '"' + s + '"' : s; is : return /^(string)$/.test(typeof s) ? '"' + s + '"' : s; (In fact, it's just ignoring the number type)
function O2String(O) { //return (jsonobj); var S = []; var J = ""; if ((O) === '[object Array]') { for (var i = 0; i < ; i++) (O2String(O[i])); J = '[' + (',') + ']'; } else if ((O) === '[object Date]') { J = "new Date(" + () + ")"; } else if ((O) === '[object RegExp]' || (O) === '[object Function]') { J = (); } else if ((O) === '[object Object]') { for (var i in O) { O[i] = typeof (O[i]) == 'string' ? '"' + O[i] + '"' : (typeof (O[i]) === 'object' ? O2String(O[i]) : O[i]); ('"' + i + '":' + O[i]); } J = '{' + (',') + '}'; } return J; };
(jsonobj)
(jsonobj), originally the easiest method, but there are browser compatibility issues (only applicable to IE8+, Chrome 1+, FF 3+)
The above is the entire content of the simple implementation of Javascript json object and string that the editor brings to you. I hope it will be helpful to you and support me more~