本文要推荐的[ToolFk]是一款程序员经常使用的线上免费测试工具箱,ToolFk 特色是专注于程序员日常的开发工具,不用安装任何软件,只要把内容贴上按一个执行按钮,就能获取到想要的内容结果。ToolFk还支持  BarCode条形码在线生成、 QueryList采集器、 PHP代码在线运行、 PHP混淆、加密、解密、 Python代码在线运行JavaScript在线运行YAML格式化工具HTTP模拟查询工具HTML在线工具箱JavaScript在线工具箱CSS在线工具箱JSON在线工具箱Unixtime时间戳转换Base64/URL/Native2Ascii转换CSV转换工具箱XML在线工具箱WebSocket在线工具Markdown 在线工具箱Htaccess2nginx 转换进制在线转换在线加密工具箱在线伪原创工具在线APK反编译在线网页截图工具在线随机密码生成在线生成二维码Qrcode在线Crontab表达式生成在线短网址生成在线计算器工具。等20多个日常程序员开发工具,算是一个非常全面的程序员工具箱网站。

網站名稱:ToolFk
網站鏈結:https://www.toolfk.com/
工具链接:https://www.toolfk.com/tool-format-json

代码教學

本工具[在线JSON格式化、JSON转CSV、JSON转XML]主要依赖下面三个JS库

https://github.com/nashwaan/xml-js#readme

https://github.com/evanplaice/jquery-csv

https://www.oschina.net/code/snippet_119610_11825

页面如下

STEP 1

STEP 2

核心代码如下

 <script type="text/javascript">
        var excerptRows = 7;
        function renderCSV(objects) {
            var rows = $.csv.fromObjects(objects, {justArrays: true});
            if (rows.length < 1) return;

            var table = document.createElement("table");
            var thead = document.createElement("thead");
            var tr = document.createElement("tr");
            var header = rows[0];
            for (field in header) {
                var th = document.createElement("th");
                $(th).text(header[field]);
                tr.appendChild(th);
            }
            thead.appendChild(tr);

            var tbody = document.createElement("tbody");
            for (var i=1; i<rows.length; i++) {
                tr = document.createElement("tr");
                for (field in rows[i]) {
                    var td = document.createElement("td");
                    $(td)
                        .text(rows[i][field])
                        .attr("title", rows[i][field]);
                    tr.appendChild(td);
                }
                tbody.appendChild(tr);
            }

            table.appendChild(thead);
            table.appendChild(tbody);
            return table.outerHTML;
        }
        function json_to_csv(message){
            var json = jsonFrom(message);
            if (json) {

                var inArray = arrayFrom(json);

                var outArray = [];
                for (var row in inArray)
                    outArray[outArray.length] = parse_object(inArray[row]);
                var value =  renderCSV(outArray.slice(0, excerptRows));
                toolfk.report('json_to_csv',value);
                return value;

            } else {
                var msg = "@lang('toolfk.lang_text_valid_txt')";
                layer.msg(msg);
                return msg;
            }
        }

        function json_to_xml(message){
           var value=  json2xml(message, {compact: true, spaces: 4});
           toolfk.report('json_to_xml',value);
           return value;
        }


        $(function(){
            var sourceArea = toolfk.create_codeMirror('code');
            var targetArea = toolfk.create_codeMirror('result','text/html');

            $("#CollapsibleView").click(function(){
                $id("CollapsibleViewDetail").style.visibility = $id("CollapsibleView").checked ? "visible" : "hidden";
                var source = sourceArea.getValue();
                if(source==''){
                    return layer.msg('@lang('toolfk.lang_context_not_empty')');
                }
                $(targetArea.getWrapperElement()).hide();
                Process(source);
            });

            $("#QuoteKeys").click(function(){
                window.QuoteKeys = $id("QuoteKeys").checked;
                var source = sourceArea.getValue();
                if(source==''){
                    return layer.msg('@lang('toolfk.lang_context_not_empty')');
                }
                $(targetArea.getWrapperElement()).hide();
                Process(source);
            });


            $("#TabSize").click(function(){
                var source = sourceArea.getValue();
                if(source==''){
                    return layer.msg('@lang('toolfk.lang_context_not_empty')');
                }
                $(targetArea.getWrapperElement()).hide();
                Process(source);
            });

            $(".convert-btn-format").click(function(){
                if (toolfk.beautify_default.beautify_in_progress) {
                    return;
                }
                var source = sourceArea.getValue();
                if(source==''){
                    return layer.msg('@lang('toolfk.lang_context_not_empty')');
                }
                toolfk.beautify_default.beautify_in_progress = true;
                $(targetArea.getWrapperElement()).hide();
                Process(source);
                toolfk.beautify_default.beautify_in_progress = false;
            });


            $(".convert-btn").click(function(){
                if (toolfk.beautify_default.beautify_in_progress) {
                    return;
                }
                var source = sourceArea.getValue();
                if(source==''){
                    return layer.msg('@lang('toolfk.lang_context_not_empty')');
                }
                toolfk.beautify_default.beautify_in_progress = true;
                $(targetArea.getWrapperElement()).show();
                $("#Canvas").hide();

                var func_method = $(this).data("func");

                var func        =   eval(func_method);
                var result      =   func(source);
                targetArea.setValue(result);
                toolfk.beautify_default.beautify_in_progress = false;
            });

            $(".copy-code").click(function(){
                toolfk.code_code({
                    source:sourceArea,
                    target:targetArea,
                });
            });
        });
    </script>

值得一試的三個理由:

  1. 整合各種程序员开发中经常使用的开发测试工具。

  2. 简洁美观大气的网站页面

  3. 支持 在线格式化执行代码、APK在线反编译、在线高强度密码生成、在线网页截图 等二十多種工具服务