<html>
  <head>
    <title>Geoserver GetFeatureInfo output</title>
  </head>
  <style type="text/css">
    table.featureInfo, table.featureInfo td, table.featureInfo th {
        border:1px solid #ddd;
        border-collapse:collapse;
        margin:0;
        padding:0;
        font-size: 90%;
        padding:.2em .1em;
    }
    table.featureInfo th {
        padding:.2em .2em;
        font-weight:bold;
        background:#eee;
    }
    table.featureInfo td{
        background:#fff;
    }
    table.featureInfo tr.odd td{
        background:#eee;
    }
    table.featureInfo caption{
        text-align:left;
        font-size:100%;
        font-weight:bold;
        padding:.2em .2em;
    }
  </style>
  <body>

<table class="featureInfo">
  <caption class="featureInfo">lx_g</caption>
  <tr>
  <th>fid</th>
    <th >ldbm</th>
    <th >lxbm</th>
    <th >lxmc</th>
    <th >ldxlh</th>
    <th >xzqh</th>
    <th >qdmc</th>
    <th >qdzh</th>
    <th >qdwz_jd</th>
    <th >qdwz_wd</th>
    <th >qdsfwfjd</th>
    <th >qdfjdlb</th>
    <th >zdmc</th>
    <th >zdzh</th>
    <th >zdwz_jd</th>
    <th >zdwz_wd</th>
    <th >zdsfwfjd</th>
    <th >zdfjdlb</th>
    <th >ldjsdj</th>
    <th >ldlmlx</th>
    <th >ldlc</th>
    <th >lmkd</th>
    <th >ljkd</th>
    <th >sfwcfld</th>
    <th >cf_ldxlh</th>
    <th >cf_lxbm</th>
    <th >sfqytc</th>
    <th >sfdtl</th>
    <th >dtllb</th>
    <th >jcsj</th>
    <th >zjgjsj</th>
    <th >hdsl</th>
    <th >klhlc</th>
    <th >ylhlc</th>
    <th >yhlc</th>
    <th >sfwcgld</th>
    <th >k5222</th>
    <th >k0306</th>
    <th >k0404</th>
    <th >k9508</th>
    <th >k0130</th>
    <th >k0131</th>
    <th >a0130</th>
    <th >a0102</th>
    <th >a0523</th>
    <th >k3901</th>
    <th >k3902</th>
    <th >k0116</th>
    <th >bz</th>
    <th >tbdw</th>
    <th >tbdwdm</th>
    <th >state</th>
    <th >id</th>
    <th >shape_len</th>
  </tr>

    <tr>

  <td>lx_g.33</td>
      <td>G351000000003</td>
      <td>G351000000</td>
      <td>台小线</td>
      <td>003</td>
      <td>420222000000</td>
      <td>阳新县枫林街道</td>
      <td>849.93100000</td>
      <td>115.39126676</td>
      <td>29.76396665</td>
      <td>0</td>
      <td>0</td>
      <td>阳新县祝家笼</td>
      <td>854.82300000</td>
      <td>115.34496667</td>
      <td>29.75813334</td>
      <td>0</td>
      <td></td>
      <td>3</td>
      <td>1</td>
      <td>4.89200000</td>
      <td>9.00000000</td>
      <td>10.00000000</td>
      <td></td>
      <td></td>
      <td></td>
      <td>1</td>
      <td>0</td>
      <td></td>
      <td>2006</td>
      <td>2015</td>
      <td>0</td>
      <td>4.88500000</td>
      <td>4.88500000</td>
      <td>4.89200000</td>
      <td>0</td>
      <td>24.00000000</td>
      <td>60</td>
      <td>2</td>
      <td>3</td>
      <td>1</td>
      <td>1</td>
      <td>阳新县公路管理局</td>
      <td>阳新县公路管理局</td>
      <td>0E-8</td>
      <td></td>
      <td></td>
      <td>20061010</td>
      <td></td>
      <td></td>
      <td></td>
      <td>3</td>
      <td>8ef86477-9cad-11ea-89b9-1062e5c81161</td>
      <td>0.0496776337732</td>
  </tr>
</table>
<br/>

  </body>
</html>

解决方案

$.ajax({
        url:"data.txt",
        success:function (res) {
            console.log(res);
            var dom = new DOMParser().parseFromString(res, 'text/html');

            var jsondata = [];
            // table head
            var heads = [];
            $(dom).find("table th").each(function(index,item){
                heads.push($(item).text());
            });
            console.log(heads);
            // tbody
            $(dom).find("table tr").each(function(index, item){
                var rowdata = {};
                $(item).find('td').each(function(index,item){
                    /*if($(item).find('[josnval]').size()>0){
                        console.log("have json val");
                        rowdata[heads[index]] = $(item).find('[josnval]').val();
                    }else{
                        console.log("no jsonval");
                        rowdata[heads[index]] = "";
                    }*/
                    if($(item).text()==null || $(item).text()==""){
                        rowdata[heads[index]] = "";
                    }else{

                        rowdata[heads[index]] = $(item).text();
                    }

                });
                jsondata.push(rowdata);
            });
            console.log(jsondata);
        }
    });
Last modification:June 18th, 2020 at 09:55 am
如果觉得我的文章对你有用,请随意赞赏