Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

5 Answers

up vote 3 down vote accepted
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);
});
share|improve this answer
1  
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 0:59
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

share|improve this answer

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/

share|improve this answer
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 1: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 1:07
thanks for tips. really helpful. i need to check if now i can simplify my code. – gerpaick Jun 15 '12 at 7:20

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);
});
share|improve this answer
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);
}); 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.