将json转换为任何多维的数组
Convert json to array for any multi dimensions?
我想知道是否有人知道将json数组转换为javascript数组的JS函数。。
返回的json数组可以是多维的,也可以是一维的。。。。所以我想找到一个函数来构造javascript数组,以满足所有维度的需求?
编辑:请不要建议使用与库相关的功能,我不使用它们。
现代浏览器已经在这么做了:
JSON.parse('["foo", ["bar", ["foofoo", "barbar"]]]');
https://developer.mozilla.org/En/Using_native_JSON
但您可能想要支持旧浏览器的东西:http://api.jquery.com/jQuery.parseJSON/
如果你的浏览器没有这个功能,我曾经在某个地方找到过这个功能,它启用了JSON.stringify和JSON.parse:
var JSON;
JSON||(JSON=function(){function v(x){return x<10?"0"+x:x}Date.prototype.toJSON=function(){return this.getUTCFullYear()+"-"+v(this.getUTCMonth()+1)+"-"+v(this.getUTCDate())+"T"+v(this.getUTCHours())+":"+v(this.getUTCMinutes())+":"+v(this.getUTCSeconds())+"Z"};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()};var i=/['u0000'u00ad'u0600-'u0604'u070f'u17b4'u17b5'u200c-'u200f'u2028-'u202f'u2060-'u206f'ufeff'ufff0-'uffff]/g,m=/['''"'x00-'x1f'x7f-'x9f'u00ad'u0600-'u0604'u070f'u17b4'u17b5'u200c-'u200f'u2028-'u202f'u2060-'u206f'ufeff'ufff0-'uffff]/g,p,
c,y={"'u0008":"''b","'t":"''t","'n":"''n","'u000c":"''f","'r":"''r",'"':'''"',"''":"''''"},C;function I(x){m.lastIndex=0;return m.test(x)?'"'+x.replace(m,function(E){var u=y[E];if(typeof u==="string")return u;return"''u"+("0000"+(+E.charCodeAt(0)).toString(16)).slice(-4)})+'"':'"'+x+'"'}function J(x,E){var u,A,s=p,t,w=E[x];if(w&&typeof w==="object"&&typeof w.toJSON==="function")w=w.toJSON(x);if(typeof C==="function")w=C.call(E,x,w);switch(typeof w){case "string":return I(w);case "number":return isFinite(w)?
String(w):"null";case "boolean":case "null":return String(w);case "object":if(!w)return"null";p+=c;t=[];if(typeof w.length==="number"&&!w.propertyIsEnumerable("length")){A=w.length;for(x=0;x<A;x+=1)t[x]=J(x,w)||"null";E=t.length===0?"[]":p?"['n"+p+t.join(",'n"+p)+"'n"+s+"]":"["+t.join(",")+"]";p=s;return E}if(C&&typeof C==="object"){A=C.length;for(x=0;x<A;x+=1){u=C[x];if(typeof u==="string")if(E=J(u,w))t.push(I(u)+(p?": ":":")+E)}}else for(u in w)if(Object.hasOwnProperty.call(w,u))if(E=J(u,w))t.push(I(u)+
(p?": ":":")+E);E=t.length===0?"{}":p?"{'n"+p+t.join(",'n"+p)+"'n"+s+"}":"{"+t.join(",")+"}";p=s;return E}}return{stringify:function(x,E,u){E=E;u=u;var A;c=p="";if(typeof u==="number")for(A=0;A<u;A+=1)c+=" ";else if(typeof u==="string")c=u;if((C=E)&&typeof E!=="function"&&(typeof E!=="object"||typeof E.length!=="number"))throw new Error("JSON.stringify");return J("",{"":x})},parse:function(x,E){var u=E;function A(s,t){var w,M,H=s[t];if(H&&typeof H==="object")for(w in H)if(Object.hasOwnProperty.call(H,
w)){M=A(H,w);if(M!==undefined)H[w]=M;else delete H[w]}return u.call(s,t,H)}i.lastIndex=0;if(i.test(x))x=x.replace(i,function(s){return"''u"+("0000"+(+s.charCodeAt(0)).toString(16)).slice(-4)});if(/^['],:{}'s]*$/.test(x.replace(/''(?:["'''/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"'''n'r]*"|true|false|null|-?'d+(?:'.'d*)?(?:[eE][+'-]?'d+)?/g,"]").replace(/(?:^|:|,)(?:'s*'[)+/g,""))){x=eval("("+x+")");return typeof u==="function"?A({"":x},""):x}throw new Error("JSON.parse");}}}());
JSON.parse=function(){var v=JSON.parse;return function(i,m){try{return v(i,m)}catch(p){return false}}}();
使用
var bob = {
key1: "val1",
key2: "val2"
}
console.log(JSON, JSON.stringify(bob), JSON.parse(JSON.stringify(bob)))
相关文章:
- 使用数据数组创建多个类似组件
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 如何在 Angular JS 中从关联数组创建多个复选框
- 使用mysql为JSON php数组分配多个列值
- 计算数组中多个对象的属性
- 对 redux 缩减器中的数组进行多次更新
- 使用一个键的值数组过滤多个javascript对象
- 解析-在数组一对多关系中获取父项及其子项
- Javascript数组对多个键进行排序是否会破坏以前的排序
- 一次更改数组的多个值的最短方法
- 如何将数组作为多个参数发送给函数
- 数组按多个值排序
- 使用_id引用数组对多个相应文档进行高效查询
- Jquery:获取数组中多个文件元素的 ID
- 将数组的多个项目映射到单个对象/函数
- javascript数组比较多次循环
- 自然排序、对象数组、多列、反向等
- 从数组创建多维对象,将该数组中的值指定为新对象中的键
- 如何按值删除数组的多个项
- 如何获取数组中多个文件的值而不需要任何循环语句