zoukankan      html  css  js  c++  java
  • html字符串 转 json

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>lcx</title>
    </head>
    <body>
    
    html:
    <textarea style=" 600px;height: 300px;font-size: 20px;" class="userhtml" ></textarea>
    
    <button onclick="html2jsonstr();">html2jsonstr</button>
    
    <div class="result" />
    
    <script>
    
    var h = 'svg width="100%" height="100%" viewBox="0 0 370 490"';
    
    var leve1 = "&nbsp; &nbsp;&nbsp;&nbsp;";
    
    var leve2 = "&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;";
    
    function html2jsonstr(){
        
        var userhtml = document.querySelector(".userhtml").value;
        var result = document.querySelector(".result");
        
        var json = "{<br/>";
        
        var patt2=/[A-Za-z]+/;
        var name = patt2.exec(userhtml);
        
        json = json + leve1 + "name: "" + name + "",<br/>";
        
        json = json + leve1 + "attrs: {<br/>";
            
        patt2=/([A-Za-z]+)=("[^"]+")/g;
        var data = {};
        while(data = patt2.exec(userhtml)){
            json = json + leve2 + data[1] + ": " + data[2] + ",<br/>";
        }
        
        json = json + leve1 + "}<br/>";
        json = json + "}<br/>";
    
        result.innerHTML = json;
    }
    
    function html2json(){
        
        var h = document.querySelector(".userhtml");
        
        var json = {};
        
        var patt2=/[A-Za-z]+/;
        var name = patt2.exec(h);
        json.name = name;
        json.attrs = {};
            
        patt2=/([A-Za-z]+)="([^"]+)"/g;
        var data = {};
        while(data = patt2.exec(h)){
            json.attrs[data[1]] = data[2];
        }
    
        console.log(json);
    }
    
    </script>
    
    </body>
    </html>
  • 相关阅读:
    linux下svn命令大全
    php常用函数
    在centos上设置计划任务
    sphinx使用心得
    sphinx2.8.8的配置文件
    Mac使用
    sftp
    uwp应用在debug模式下运行正常,编译为release版本的时候抛出异常
    win10 uwp 读取resw资源文件
    dll被设置为用记事本打开的解决方法
  • 原文地址:https://www.cnblogs.com/LcxSummer/p/14707719.html
Copyright © 2011-2022 走看看