首页 > 代码库 > Js Json 互转
Js Json 互转
推荐:
//js对象转换为 JSON 文本var text = ‘[{"id":1,"name":"C","size":"","date":"02/19/2010","children":[{"id":2,"name":"Program Files","size":"120 MB","date":"03/20/2010","children":[{"id":21,"name":"Java","size":"","date":"01/13/2010","state":"closed","children":[{"id":211,"name":"java.exe","size":"142 KB","date":"01/13/2010"},{"id":212,"name":"jawt.dll","size":"5 KB","date":"01/13/2010"}]},{"id":22,"name":"MySQL","size":"","date":"01/13/2010","state":"closed","children":[{"id":221,"name":"my.ini","size":"10 KB","date":"02/26/2009"},{"id":222,"name":"my-huge.ini","size":"5 KB","date":"02/26/2009"},{"id":223,"name":"my-large.ini","size":"5 KB","date":"02/26/2009"}]}]},{"id":3,"name":"eclipse","size":"","date":"01/20/2010","children":[{"id":31,"name":"eclipse.exe","size":"56 KB","date":"05/19/2009"},{"id":32,"name":"eclipse.ini","size":"1 KB","date":"04/20/2010"},{"id":33,"name":"notice.html","size":"7 KB","date":"03/17/2005"}]}]}]‘;var obj= JSON.parse(text);//可选的 filter 参数将遍历每一个value key 值对, 并进行相关的处理, 如:/*var obj= JSON.parse(text, function (key, value) { return key.indexOf(‘date‘) >= 0 ? new Date(value) : value; });*///JSON字符串转化为对象var jsonText = JSON.stringify(obj);
其他:
var t = "{‘firstName‘: ‘cyra‘, ‘lastName‘: ‘richardson‘, ‘address‘: { ‘streetAddress‘: ‘1 Microsoft way‘, ‘city‘: ‘Redmond‘, ‘state‘: ‘WA‘, ‘postalCode‘: 98052 },‘phoneNumbers‘: [ ‘425-777-7777‘,‘206-777-7777‘ ] }";var jsonobj = eval(‘(‘ + t + ‘)‘);var t2 = "[{name:‘zhangsan‘,age:‘24‘},{name:‘lisi‘,age:‘30‘},{name:‘wangwu‘,age:‘16‘},{name:‘tianqi‘,age:‘7‘}] ";var myobj = eval(t2);
Js Json 互转
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。