以json格式获取表数据

时间:2017-06-02 17:57:50

标签: javascript html arrays json html5

大家好,我试图以json格式获取表格数据,这是我的表格

   <table>
  <thead>
    <tr>
      <th>srno</th>
      <th>name</th>
      <th>email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Jhon One</td>
      <td>Doe one</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Jhon two</td>
      <td>Doe Two</td>
    </tr>
  </tbody>
</table>

<button>
  convert
</button>

我得到的结果就是这个

{
    "0": {
        "1",
        "Jhon One",
        "Doe one"
    }
    ,
    "1": {
        "2",
        "Jhon two",
        "Doe Two"
    }
}

使用以下javascript

$("button").click(function() {
  var json = html2json();
  alert(json);
});

function html2json() {
  var json = '{';
  var otArr = [];
  // var i = 1;
  var tbl2 = $('table tbody tr').each(function(e) {
    x = $(this).children();
    var itArr = [];
    x.each(function() {
      itArr.push('"' + $(this).text() + '"');
    });
    otArr.push('"' + e + '": {' + itArr.join(',') + '}');
  })
  json += otArr.join(",") + '}'

  return json;
}

但是我想为每个值添加键,数字应该从1开始而不是零。

我有一组欲望结果,它应该看起来像这样 任何帮助表示赞赏

  {
        "1": {
           no: "1",
           name:"Jhon One",
           lastname "Doe one"
        }
        ,
         "2": {
           no: "1",
           name:"Jhon two",
           lastname "Doe two"
        }

    }

这是我尝试过的fiddel链接

https://jsfiddle.net/k228n2bn/

4 个答案:

答案 0 :(得分:2)

只需更改以下行

即可
str(ins.compile(dialect=oracle.dialect(), compile_kwargs={'literal_binds': True}))

INSERT INTO my_table (a, b, c) SELECT my_table2.d, bar.e, bar.f 
FROM my_table2 JOIN (SELECT my_table3.e AS e, max(my_table3.f) AS f, count(my_table3.g) AS g 
FROM my_table3 
WHERE my_table3.h = 'foo' GROUP BY my_table3.e 
HAVING count(my_table3.g) = 1) bar ON my_table2.g = bar.g

括号将值添加为数字而不是字符串。

此外,为内部对象键添加INSERT INTO my _table (a, b c) SELECT my_table2.d, bar.e, bar.f FROM my_table2 JOIN ( SELECT my_table3.e, max(my_table3.f), count(my_table3.g) FROM my_table3 WHERE my_table3.h = 'foo' GROUP BY my_table3.e HAVING count(my_table3.g) = 1 ) bar ON my_table2.g = bar.g 数组。

otArr.push('"' + e + '": {' + itArr.join(',') + '}');

答案 1 :(得分:1)

您可以将<!DOCTYPE HTML> <html> <head> <title>Inzendopdracht 051R4</title> <meta charset="utf-8" /> <link rel="stylesheet" href="style.css" type="text/css" media="all" /> </head> <body> <a href='viewguestbook.php'>View Guestbook</a> <?php // define variables and set to empty values $nameErr = $sportErr = $beoefenaarErr = $textErr = ""; $name = $sport = $beoefenaar = $text =""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $valid = true; if (empty($_POST["name"])) { $nameErr = "Name is required"; $valid = false; } else { $name = test_input($_POST["name"]); } if (empty($_POST["sport"])) { $sportErr = "Sport is required"; $valid = false; } else { $sport = test_input($_POST["sport"]); } if (empty($_POST["text"])) { $textErr = "Comment is required"; $valid = false; } else { $text = test_input($_POST["text"]); } if (empty($_POST["beoefenaar"])) { $beoefenaarErr = "Comment is required"; } else { $beoefenaar = test_input($_POST["beoefenaar"]); } if($valid){ header("location: http://localhost/051R4-verwerk.php"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>Laat hier u bericht achter</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> Sport: <select name="sport"> <option valeu=""> <option valeu="Tennis">Tennis <option valeu="Voetbal">Voetbal <option valeu="Running">Running <option valeu="Tafeltenis">Tafeltenis <option valeu="Squash">Squash <option valeu="Wielrennen">Wielrennen <option valeu="Boksen">Boksen </select> <span class="error">* <?php echo $sportErr;?></span> <br><br> Beoefenaar: <INPUT TYPE="radio" name="beoefenaar" value="0" checked>Nee <INPUT TYPE="radio" name="beoefenaar" value="1">Ja <br><br> <textarea name="text" placeholder="Schrijf hier u bericht*" /></textarea> <span class="error"> <?php echo $textErr;?></span> <br><br> <input type="submit" value="Verstuur"/> </form> </body> </html> 转换为数字,然后像this fiddle一样添加一个数字。

e

你回来的json虽然无效。如果您可以简化并确保有效的json并从任何表结构创建对象,则可能需要执行this fiddle之类的操作。

function html2json() {
  var json = '{';
  var otArr = [];
  // var i = 1;
  var tbl2 = $('table tbody tr').each(function(e) {
    x = $(this).children();
    var itArr = [];
    x.each(function() {
      itArr.push('"' + $(this).text() + '"');
    });
    otArr.push('"' + (Number(e) + 1) + '": {' + itArr.join(',') + '}');
  })
  json += otArr.join(",") + '}'

  return json;
}

答案 2 :(得分:0)

尝试e+1

更改otArr.push('"' + e + '": {' + itArr.join(',') + '}');


otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');

$("button").click(function() {
  var json = html2json(); 
});

function html2json() {
  var json = '{';
  var otArr = [];
   
  var tbl2 = $('table tbody tr').each(function(e) {
    x = $(this).children();
    var itArr = [];
    x.each(function(e) {
   
      var items='';
       if(e == 0){
          items +='no : "'+ $(this).text()+'"';
          
       } 
       if(e == 1){
          items +='name : "' +$(this).text()+'"'; 
       }
       if(e == 2){
          items +='email : "' +$(this).text()+'"'; 
       }
       
       itArr.push(items);
       
    });
   
    otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');
  })
  json += otArr.join(",") + '}'
alert(json);
  return json;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>srno</th>
      <th>name</th>
      <th>email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Jhon One</td>
      <td>Doe one</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Jhon two</td>
      <td>Doe Two</td>
    </tr>
  </tbody>
</table>

<button>
  convert
</button>

答案 3 :(得分:0)

也许你可以使用theads作为生成对象的键。 检查this jsfiddle

function html2json() {
  var $table = $('table');
  var $ths = $table.find('thead>tr>th');
  var rows = {};
  $table.find('tbody>tr').each(function () {
    var row = {};
    $(this).children().each(function (index) {
      row[$ths[index].textContent] = this.textContent;
    });
    rows[row.srno] = row;
  });
  return JSON.stringify(rows);
}