SuiFei
人的一生,或多或少,总是难免有浮沉。不会永远如旭日东升,也不会永远痛苦潦倒。只有面对现实,才能超越现实
博客园
首页
博问
闪存
新随笔
联系
订阅
管理
随笔-154 文章-3 评论-1316
2010年5月9日
51js 的json编辑器
无忧JSON编辑器【辅助工具】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <base href="http://imgqun.qq.com/cgi-bin/"> <title> ldh_JSON_Editor </title> <style> body,html{ margin:0px;padding:10px;overflow:hidden;font-size:12px; background:#FFCC66 url(img?uuid=20b6767c202cdabf6049e98539e8df0de6);color:#333;font-family:tahoma; } .title{font-size:14px;background:url(img?uuid=2069cf3fd1e53a2bb365f771eac65b50a2) no-repeat;padding-left:30px;} #ldh_ui_window{ width:840px;height:520px; position:absolute; background:#fff url(img?uuid=209ef12ed3496ca2e94cf1a8041de28cc2) repeat-x 0 32px; border:2px solid #4A84C4;border-top:none; left:50%;margin-left:-420px; top:50%;margin-top:-260px; } #ldh_ui_caption{ height:32px;line-height:32px;text-indent:1em; background:url(img?uuid=203fe9e79d8933c63af9866344a990e5ee) repeat-x; text-align:center; } #ldh_ui_body_left{ width:300px;height:450px;overflow:auto; position:absolute;left:10px;top:55px; border:1px solid #4A84C4; } #editWindow{width:500px;position:absolute;left:325px;top:55px; border:1px solid #4A84C4;} #tree_cap{height:18px;line-height:18px;text-align:center;background:url(img?uuid=203fe9e79d8933c63af9866344a990e5ee) repeat-x;} /* toolbar */ #subToolbar{ height:29px;background:url(img?uuid=2089a60bdc587e2b2b895c3cb32608c06f) repeat-x 50% 50%;line-height:29px; position:relative;width:100%;text-indent:10px; } #subToolbar a{ color:#000;background:url(img?uuid=2023acf6c478a05549e3cdf0de3696ca7d) no-repeat 0% 50%; padding-left:20px;text-decoration:none; } #subToolbar span{position:absolute; right:10px;bottom:4px;} #subToolbar a:hover{color:#fff;background-image:url(format.gif);text-decoration:underline;} #subToolbar a#save_as{background-image:url(img?uuid=2083fbd06db4030248c60ab536af05def2); } #subToolbar a#format_indent{background-image:url(img?uuid=2060be6af87eae7a41f7de458b271cdfdc); } #subToolbar a#update{background-image:url(img?uuid=209d82f0afde195855dca5a2b516fdcc11); } #subToolbar a#format_compress{background-image:url(img?uuid=20e66dfd444ae20969e4bfcc0d33809850); } #subToolbar a#clear_txt{background-image:url(img?uuid=2069999c20b5d5f9fa36439831690cbea1); } /* end */ #json_eidit{ width:497px; border:none;margin:0px;color:#000; height:340px;font-size:14px; } .json_editInfo{ line-height:160%; border:1px solid #4A84C4; width:483px;+width:500px; height:65px; color:#003300; position:absolute;left:325px;bottom:12px; } #json_editInfo{ height:48px; padding-left:60px;padding-top:10px; background:url(img?uuid=2097679a52658b38bc858e8274a2293451) no-repeat 28px 12px; } #json_editInfo b{color:red} #ldh_ui_window .err{color:red;background-image:url(img?uuid=20bded9fd31a0fea0465715c35f1d59c45);} #ldh_ui_window .busy{color:#333;background-image:url(img?uuid=20c121f445de7a8874541ef58b4147c0e4);} #ldh_ui_window .info{color:#006600;background-image:url(img?uuid=20775d53781369b7d2a02b6faeb1693a5c);} #json_editInfo input{width:50px;border:1px solid #4A84C4;height:14px;} #json_editInfo button{ background:url(img?uuid=200913d75fd547b62c50e13d8884bc1d98) no-repeat; width:70px;height:20px;line-height:20px;text-align:center; border:none;color:#114060;letter-spacing:5px;margin-left:10px; } /* for tree */ img,input,select{vertical-align:middle} #ldh_ui_window #tree{white-space:nowrap;font-size:12px;line-height:24px;} #tree a{text-decoration:none;color:#333;} #tree a:hover,#tree a.hot{color:#000;background:#0B92FF;} dd,dl,dt{padding:0;margin:0;border:none;font-size:12px;} dt img{vertical-align:middle;} dt{height:20px;white-space:nowrap;} </style> </head> <body> <div id="ldh_ui_window"> <div id="ldh_ui_caption"><strong class="title">JSON-Editor</strong></div> <div id="ldh_ui_body_left"> <div id="tree_cap">树视图</div> <div id="tree"></div> </div> <div id="editWindow"> <div id="subToolbar"> <a href="javascript:void(0)" title="" id="format_indent">缩进</a> | <a href="javascript:void(0)" title="" id="format_compress">紧凑</a> | <a href="javascript:void(0)" title="" id="update">刷新视图</a> | <a href="javascript:void(0)" title="" id="clear_txt">清空</a> | <a href="javascript:void(0)" title="" id="save_as">另存为</a> <span> <label for="autoUpdate"><input type="checkbox" id="autoUpdate" checked /> 同步更新树视图</label> </span> </div> <div id="edit"> <textarea id="json_eidit" >[{"中国食谱":["上海醉蟹","北京锅烧鸡","川式凉拌豇豆","清真酱牛肉"],"国外食谱":["泰式柠檬肉片","鸡柳汉堡","蒸桂鱼卷 "],"更多":{"中式":["南","北",{"地方":["小吃","大餐"]},"更多选择"]}},{"菜谱分类":["上海菜","贵州菜","潮汕菜","云南菜","东北菜","安徽菜","广东菜","浙江菜","湖南菜"]},"其它"]</textarea> </div> </div> <fieldset class="json_editInfo"> <legend>系统消息</legend> <div id="json_editInfo" class="busy">正在加载图片,请稍候...</div> </fieldset> </div> </body> <script> /* main Object */ JE={ data:{},/* 关联数据 */ code:false,/* 格式化后的代码 */ oldCode:[],/* 历史代码 */ editUI:null,/* 关联编辑框 */ msgUI:null,/* 信息显示窗口 */ treeUI:null,/* 树窗口 */ name:'JE',/* 实例名 */ root:'<b>JSON无忧</b>',/* 根节点标题 */ checkbox:0,/* 是否添加复框 */ hot:null,/* 选中节点 */ indent:' ',/*缩进符'\t'或者4个' '*/ firstUp:true,/*第一次自动刷新*/ onclick:Array,/*树点击通知*/ countInfo:'',/*统计信息*/ formating:false,/* 格式化中(禁止重构树) */ ico:{/* 树图标 */ //文件夹结构线 r0:'img?uuid=20166b3094daba4bc6e18817b8301b093a', r0c:'img?uuid=208018827ed877e31810e838d33e4ac2b0', r1:'img?uuid=2094c793496278bde84be80bb6cb2117f5', r1c:'img?uuid=205f2329c537dcfd32b5f33bf642f76fa6', r2:'img?uuid=20af62be7c197cae64d3e533f7aaf28c25', r2c:'img?uuid=20a005983863e5bc8544cecd8b7f82fcdb', //前缀图片 nl:'img?uuid=20f508bdc9bb8d98f4529e0fa2475b91bb', vl:'img?uuid=20d5de63f4e6927bbb23c377bd1073d26f', //文件结构线 f1:'img?uuid=202ccc639afd44130a3946e9837672479c', f2:'img?uuid=20900ceb0053a2f7bd07a22337c4e4c72c', root:'img?uuid=2069cf3fd1e53a2bb365f771eac65b50a2', //文件夹 arr:'img?uuid=20b7d6e86f4f288ea097c10b1c0d7f4b6b', arrc:'img?uuid=20b7d6e86f4f288ea097c10b1c0d7f4b6b', obj:'img?uuid=20c34d1d5d5a4639061e08165c61a97e63', objc:'img?uuid=20c34d1d5d5a4639061e08165c61a97e63', //文件 arr2:'img?uuid=20327500502b71ed0278a0cc1bf8f8bb41', obj2:'img?uuid=203346cafeedac1fb7628bc886b9b7fb47' }, toTree:function(){/* JSON转换为树HTML,同时格式化代码 */ var draw=[],This=this,ico=this.ico; JE.firstUp=false;/*完成首次自动构造*/ var notify=function(prefix/*前缀HTML*/,lastParent/*父是否是尾节点(确定图标是空白|结构线)*/,name/*节点名*/,value/*节点值*/,formObj/* 父是否是对象(确定子图标) */){/* 构造子节点 */ var rl=prefix==''?ico.r0:lastParent?ico.r1:ico.r2;/* 配置根节点图标 */ if(value&&value.constructor==Array){/* 处理数组节点 */ draw.push('<dl><dt>',This.draw(prefix,rl,ico.arr,name,''),'</dt><dd>');/* 绘制文件夹 */ for (var i=0;i<value.length;i++) notify(prefix+This.img(lastParent?ico.nl:ico.vl),i==value.length-1,i,value[i]); draw.push('</dd></dl>'); }else if(value&&typeof value=='object'){/* 处理对象节点 */ draw.push('<dl><dt>',This.draw(prefix,rl,ico.obj,name,''),'</dt><dd>');/* 绘制文件夹 */ var len=0,i=0; for(var key in value)len++;/* 获取对象成员总数 */ for(var key in value)notify(prefix+This.img(lastParent?ico.nl:ico.vl),++i==len,key,value[key],true); draw.push('</dd></dl>'); }else{/* 处理叶节点(绘制文件) */ draw.push('<dl><dt>',This.draw(prefix,lastParent?ico.f1:ico.f2,formObj?ico.obj2:ico.arr2,name,value),'</dt></dl>'); }; };/* 不是[]或者{}不绘制 */ if(typeof this.data=='object'){notify('',true,this.root,this.data);}; if(this.treeUI)this.treeUI.innerHTML=draw.join('');/* 显示在树窗口 */ this.msg('成功构造树视图!'); }, draw:function(prevfix,line,ico,name,value){/* 子项HTML构造器 */ var cmd=false,J=this.ico,imgName=false; switch (line) {//传递切换图标 case J.r0:imgName='r0';break; case J.r1:imgName='r1';break; case J.r2:imgName='r2'; } if(imgName)cmd=' onclick="'+this.name+'.show(this,\''+imgName+'\')" ';/* 加入折叠命令 */ var type=typeof name=='string'?'(对象成员)':'(数组索引)'; return prevfix+this.img(line,cmd) +(this.checkbox?'<input type="checkbox" onclick="'+this.name+'.select(this)" />':'') +this.img(ico)+' <a href="javascript:void(0)" onclick="'+this.name+'.click(this);" ' +'key="'+name+'" val="'+value+'" >' +name+type+(value==''?'':' = ')+value+'</a>' }, img:function(src,attr){/* img HTML构造 */ return '<img src="'+src+'" '+(attr||'')+' />'; }, click:function(item){/* 子项点击统一回调 */ if(this.hot)this.hot.className=''; this.hot=item; this.hot.className='hot';/* 切换选中项 */ this.onclick(item); }, format:function(txt,compress/*是否为压缩模式*/){/* 格式化JSON源码(对象转换为JSON文本) */ if(/^\s*$/.test(txt))return this.msg('数据为空,无法格式化! '); try{var data=eval('('+txt+')');} catch(e){ JE.editUI.style.color='red'; return this.msg('数据源语法错误,格式化失败! 错误信息: '+e.description,'err'); }; JE.editUI.style.color='#000'; var draw=[],last=false,This=this,line=compress?'':'\n',nodeCount=0,maxDepth=0; var notify=function(name,value,isLast,indent/*缩进*/,formObj){ nodeCount++;/*节点计数*/ for (var i=0,tab='';i<indent;i++ )tab+=This.indent;/* 缩进HTML */ tab=compress?'':tab;/*压缩模式忽略缩进*/ maxDepth=++indent;/*缩进递增并记录*/ if(value&&value.constructor==Array){/*处理数组*/ draw.push(tab+(formObj?('"'+name+'":'):'')+'['+line);/*缩进'[' 然后换行*/ for (var i=0;i<value.length;i++) notify(i,value[i],i==value.length-1,indent,false); draw.push(tab+']'+(isLast?line:(','+line)));/*缩进']'换行,若非尾元素则添加逗号*/ }else if(value&&typeof value=='object'){/*处理对象*/ draw.push(tab+(formObj?('"'+name+'":'):'')+'{'+line);/*缩进'{' 然后换行*/ var len=0,i=0; for(var key in value)len++; for(var key in value)notify(key,value[key],++i==len,indent,true); draw.push(tab+'}'+(isLast?line:(','+line)));/*缩进'}'换行,若非尾元素则添加逗号*/ }else{ if(typeof value=='string')value='"'+value+'"'; draw.push(tab+(formObj?('"'+name+'":'):'')+value+(isLast?'':',')+line); }; }; var isLast=true,indent=0; notify('',data,isLast,indent,false); this.countInfo='共处理节点<b>'+nodeCount+'</b>个,最大树深为<b>'+maxDepth+'</b>'; return draw.join(''); }, msg:function(text,type){/* 编辑状态或者错误通知 */ if(!this.msgUI)return alert(text); with(new Date)var now=([getHours(),getMinutes(),getSeconds()].join(':')).replace(/\b\d\b/g,'0$&'); this.msgUI.innerHTML='<div>['+now+'] '+text.replace(/\n/g,'<br/>')+'</div>'; this.msgUI.className=type; }, show:function (ico,id){/* 显隐树节点 */ var subView=ico.parentNode.parentNode.childNodes[1].style,J=this.ico; if(subView.display=='none'){ subView.display=''; ico.src=J[id]; }else{ subView.display='none'; ico.src=J[id+'c']; }; }, select:function (sender){ var sub=sender.parentNode.parentNode.getElementsByTagName("INPUT"); for (var i=0;i<sub.length;i++ ) {sub[i].checked=sender.checked;} } }; JE.add=function(){ this.msg('功能添加中...*_^'); } JE.editItem=function(){ this.msg('功能添加中...*_^'); } JE.begin=function(){/* 设置UI控件关联响应 */ var $=function (id){return document.getElementById(id)}; /* 关联UI */ JE.editUI=$("json_eidit"); JE.msgUI=$("json_editInfo"); JE.treeUI=$("tree"); var updateUI=$("update"); var auto=$("autoUpdate"); var fontSize=$("fontSize"); /* 单击树子项 */ JE.onclick=function(item){ var key='键名: <input value="'+item.getAttribute('key')+'" />', val=' 键值: '+(item.getAttribute('val')==''?'成员列表':'<input value="'+item.getAttribute('val')+'" />'), add='<button onclick="'+this.name+'.add(this)">新增</button>', edit='<button onclick="'+this.name+'.editItem(this)">修改</button>'; JE.msg(key+val+add+edit,'info'); } /* 监听代码变化事件 */ JE.editUI.oninput=JE.editUI.onpropertychange=function (){ if(JE.formating)return;/* 格式化不刷新树 */ if(/^\s*$/.test(this.value))return JE.msg('请输入JSON格式的代码!');; clearTimeout(JE.update); try{JE.data=eval('('+this.value+')'); }catch(e){ JE.editUI.style.color='red'; return JE.msg("源代码有错误: "+e.description+' , 如果正在编辑中, 请忽略此消息!','err'); }; JE.editUI.style.color='#000'; if(auto.checked||JE.firstUp){/*若同步*/ JE.msg('语法正确,正在重新构造树,请稍候...','busy'); JE.update=setTimeout(function(){ JE.toTree(); },450); }else{ JE.msg('语法正确,请点击刷新,或者打开视图同步开关,或者继续编辑!') } return true; }; if(window.ActiveXObject) document.execCommand("BackgroundImageCache", false, true); /* 拦截Tab,自动格式化 */ JE.editUI.onkeydown=function (){ if(event.keyCode==9){$('format_indent').onclick();event.returnValue=false;}; JE.code=this.value; } /* 格式化 */ var format=function(compress){ var code=JE.format(JE.editUI.value,compress); JE.formating=true; if(code)JE.editUI.value=code; JE.editUI.focus(); setTimeout(function(){JE.formating=false;},1000); return code; } /* 工具栏按钮 */ $('format_indent').onclick=function (){if(format())JE.msg('完成缩进风格转换,'+JE.countInfo)} $('format_compress').onclick=function (){if(format(true)!=undefined)JE.msg('完成紧凑风格转换,'+JE.countInfo);} updateUI.onclick=function (){ JE.firstUp=true; JE.editUI.onpropertychange()?JE.msg('成功刷新视图!'):JE.msg('数据有误,刷新失败!','err') JE.firstUp=false; }; $('clear_txt').onclick=function (){JE.editUI.value=JE.treeUI.innerHTML='';JE.editUI.focus();} auto.onclick=function (){JE.msg('自动同步视图功能'+(this.checked?'开启':'关闭!'));}; /* 另存为 */ if(/*@cc_on !@*/true){$('save_as').style.display='none'}; $('save_as').onclick=function (){ var d=document,w=d.createElement('IFRAME'); w.style.display="none"; d.body.appendChild(w); setTimeout(function(){ var g=w.contentWindow.document; g.charset = 'utf-8'; g.body.innerHTML=JE.editUI.value; g.execCommand("saveas",'', "json.txt") ; },1); } }; /* 从这里开始 */ window.onload=function (){ JE.begin(); } </script> </html>
提示:您可以先修改部分代码再运行
用来简单的查看、辅助修改繁杂的
JSON
数据,格式化或者压缩J
JSON
,当然也可以自由即时编辑一些简单的
JSON
数据。
大家可以输入或者粘贴一些
JSON
数据来校验是否正确。
按
Tab
键自动全文缩进格式化。
posted @ 2010-05-09 16:13 萧寒 阅读(935) 评论(0)
编辑
公告
多少事,从来急;
天地转,光阴迫。
一万年太久,只争朝夕。
昵称:
萧寒
园龄:
6年10个月
粉丝:
53
关注:
5
<
2010年5月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
最新随笔
1. 利用HtmlAgilityPack抓取网站图片并下载~~~~~~邪恶完善版
2. delphi函数,识别字符集编码
3. 常用DNS列表(电信、网通)
4. Visual studio 2010 sp1中文版正式版无法安装Silverlight5_Tools rc1 的解决办法
5. Resharper 6.x Keygen
6. 守护进程杀手(测试)
7. 开发silverlight下的xps浏览器,支持xps printer输出格式
8. visual studio 2005 连接 tfs2010
9. 软件开发报价的计算方法[转载]
10. Visual studio 2010 中文版正式版无法安装Silverlight4 Tools的解决办法
我的标签
vsts
(2)
vstscodehelper
(2)
source code
(1)
silverlight
(1)
ria
(1)
rtm
(1)
msdn
(1)
vs2010
(1)
Unit
(1)
单元测试
(1)
更多
随笔分类
(238)
.NET WebForms(19)
(rss)
C# WinForms(77)
(rss)
Delphi(10)
(rss)
Javascript(2)
(rss)
Linux(4)
(rss)
silverlight
(rss)
VS Unit(1)
(rss)
Windows 7(1)
(rss)
WPF
(rss)
连连看.NET(8)
(rss)
模拟泡泡堂(2)
(rss)
日记(34)
(rss)
视频
(rss)
收藏(10)
(rss)
外挂(8)
(rss)
小知识(45)
(rss)
游戏新闻(17)
(rss)
随笔档案
(154)
2012年2月 (1)
2012年1月 (1)
2011年11月 (1)
2011年9月 (1)
2011年7月 (1)
2010年12月 (1)
2010年9月 (1)
2010年8月 (1)
2010年7月 (1)
2010年5月 (5)
2010年4月 (6)
2010年3月 (1)
2010年2月 (2)
2010年1月 (12)
2009年12月 (1)
2009年10月 (1)
2009年9月 (1)
2009年8月 (1)
2008年12月 (1)
2008年11月 (2)
2008年9月 (1)
2008年7月 (2)
2008年6月 (2)
2008年4月 (1)
2008年3月 (1)
2008年2月 (1)
2007年11月 (1)
2007年4月 (1)
2007年3月 (1)
2006年12月 (7)
2006年9月 (4)
2006年8月 (3)
2006年7月 (5)
2006年6月 (4)
2006年4月 (3)
2006年3月 (1)
2006年2月 (1)
2006年1月 (1)
2005年12月 (6)
2005年10月 (4)
2005年9月 (1)
2005年8月 (4)
2005年7月 (6)
2005年6月 (11)
2005年5月 (18)
2005年4月 (22)
文章分类
(3)
C# 2.0(1)
(rss)
C#与游戏编程(2)
(rss)
IBM Websphere MQ
(rss)
文章档案
(3)
2006年6月 (1)
2005年5月 (2)
相册
小片段
博客园
alittlefish
(rss)
鱼遇于池,池涸,相濡以沫,相鞠以湿,不若相忘于海。
FireScript地带
(rss)
FireReprt◇FireScript地带
个人收藏
MSDN(中文)
MSDN
my msn blog.
我的msn Blog.
notreg
notreg
ubuntu 中文手册
WorldLingo
WorldLingo: 翻译、本地化、全球化
电驴下载
分享互联网
构件EOS
我在腾讯的个人文集
我在腾讯的个人文集,那时候混qq论坛,但最后腾讯取消asp版面,就结束了.
字幕搜索
射手网 - Shooter.com.cn - 与别人分享,别人与你分享
朋友
音乐虫子
(rss)
音乐虫子
相关工具
Convert C# to vbnet
This utility will allow you to convert your C# code to its equivalent in VB.NET.
Convert vbnet to C#
This utility will allow you to convert your VB.NET code to its equivalent in C#.
pinvoke
.net 的api库
看地图
卫星地图
友人[手机网]
全都是手机,还有介绍
积分与排名
积分 - 439039
排名 - 142
最新评论
阅读排行榜
评论排行榜
推荐排行榜