2

i was wondering how to parse html table to get json object that i can send via $.post with jquery.

i have a table

<table id="Report">
 <thead>
  <tr>
   <th>Kod</th>
   <th>Nazwa</th>
   <th>Ilość</th>
   <th>Netto / 1szt.</th>
   <th>Suma brutto</th>
  </tr>
 </thead>
<tbody>
 <tr>
  <td>00171 </td>
  <td>SŁUP 50/1800 POŚREDNI(P) </td>
  <td>5</td><td>97.00 PLN </td>
  <td>394.31 PLN </td>
 </tr>
 <tr>
  <td>00172</td>
  <td>SŁUP 50/1800 NAROŻNY(P)</td>
  <td>1</td><td>97.00 PLN</td>
  <td>78.86 PLN</td>
 </tr>
 <tr>
  <td>00173 </td>
  <td>SŁUP 50/1800 KOŃCOWY(P) </td>
  <td>1</td><td>97.00 PLN </td>
  <td>78.86 PLN</td>
 </tr>
</tbody>
<tfoot style="font-weight: bold;">    
 <tr>
  <th colspan="3" style="text-align: right;">Razem netto: 1955.85 PLN</th>
  <th colspan="2" style="text-align: right;">Razem brutto: 2405.69 PLN</th>
 </tr>
 </tfoot>
</table>

and what i need is json object in this format (first <td> and third <td>):

[{"00171":5},
 {"00172":1},
 {"00173":1}
]

and that i can send it via

$.post(  
 "print.php",  
 {json: myjsonvar},  
 "json"  
);

any idea how to do that?

thanks

gerpaick
  • 801
  • 2
  • 13
  • 36

5 Answers5

6
var json = [];
$('#Report').find('tbody tr').each(function(){
    var obj = {},
        $td = $(this).find('td'),
        key = $td.eq(0).text(),
        val = parseInt( $td.eq(2).text(), 10 );
    obj[key] = val;
    json.push(obj);
});
Kevin Ennis
  • 14,226
  • 2
  • 43
  • 44
  • 2
    thanks, i didnt know that it was so easy. many solutions are really similiar but your anser gives also `val` as an integer. thanks again – gerpaick Jun 15 '12 at 00:59
  • `obj.key` is more preferable than `obj[key]` – Ratul May 12 '20 at 14:28
  • 1
    `key` is not the name of a property, it is a *variable* whose *value* is the name of a property. So if you try to use `obj.key` you get the value of a property on `obj` named `key`, which will be `undefined`. Instead, you need to use `obj[ key ]`. – Kevin Ennis Jun 24 '20 at 20:02
3

How about:

var myjsonvar=[];

$('#Report tbody tr').each(function(){
   var data={};
   var tr=$(this);
   data[tr.find('td:first-child').text()]=tr.find('td:nth-child(3)').text();
   myjsonvar.push(data);
});
broesch
  • 2,442
  • 1
  • 19
  • 9
2

Why json, if you are in js already? Just create a simple object:

var data = {};

$("#Report tbody tr").each(function() {
   data[$(this).children("td:eq(0)").text()] = $(this).children("td:eq(2)").text();
});

$.post("print.php", data);

Setting type to json in $.post defines the server response to be json, not the send data!

http://jsfiddle.net/zyCPN/

d_inevitable
  • 4,381
  • 2
  • 29
  • 48
  • thanks for you reply. i asked for json object because in my php code i had already some part for parsing json objects, so it would be a little easier for me. – gerpaick Jun 15 '12 at 01:01
  • @gerpaik, `json_decode()` would do that in php, however if you submit it like above you'll have `$_POST` being `array("00171" => "5", "00172" => "1", "00173" => "1");`, without any parsing necessary. – d_inevitable Jun 15 '12 at 01:07
  • thanks for tips. really helpful. i need to check if now i can simplify my code. – gerpaick Jun 15 '12 at 07:20
2
var sendData = [];

$('#Report tbody tr').each(function(i, el){
    var key = $.trim($(this).find('td:eq(0)').text()),
        val = $.trim($(this).find('td:eq(2)').text()),
        obj = {};
    obj[key] = val;
    sendData.push(obj);
});

See demo

mVChr
  • 49,587
  • 11
  • 107
  • 104
1
var objArray=[];
$('table#Report tbody tr').each(function(i){
    var row=$(this);
    obj={};
    obj[$('td', row).eq(0).text()]=$('td', row).eq(2).text();
    objArray.push(obj);
}); 
The Alpha
  • 143,660
  • 29
  • 287
  • 307