Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to process a Json array returned by php file. The PHP code is below.

$pdo = new PDO('');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * from table';
foreach ($pdo->query($sql) as $row) 
{
    $results[] = array('row1' =>$row['col1'],
    'row2' => $row['col2'],
    'row3' => $row['col3']);
}
header('Content-Type: application/json');
echo json_encode($results);

The above code will return Json array like this one

[
   {"row1":"test","row2":"5.123456","row3":"6.123456"},
   {"row1":"test1","row2":"6.123456","row3":"8.123654"},
   {"row1":"test3","row2":"6.321456","row3":"8.964512"}
]

I want to process the above Json array and present it inside a table. I try many Jquery codes and spent hours in front of computer. But i can't get correct answer. Please help me. Help me by either give correct Jquery code to process the Json array or give correct PHP code to send correct Json string. I forgot to say another info. When I try to debug using FF, I am getting an error in console

http://i.stack.imgur.com/tnLcZ.jpg

(image shows: SyntaxError: JSON.parse: unexpected end of data)

UPDATE

I update your script like this. The script return nothing. Please help

var html="<table border=1 style='border-collapse:collapse'>";
$(document).ready(function () {
$.ajax({
   url: "http://10.44.0.160/service.php",
   contentType: "application/json; charset=utf-8",
   dataType: "jsonp",
   type: "POST",
success: function(data) 
{
data.forEach(function(obj,index){html+="<tr><td>"+index+"<td>"+obj.csc_name+"</td><td>"+obj.lat+"</td><td>"+obj.longi+"</td></tr>";});
html+="</table>";
jQuery(html).appendTo("body");
}
            });
        });
share|improve this question
    
try looking at ExtJS , it accepts the data as JSON , will display in whatever format you want –  Satya Oct 4 '13 at 10:34
    
I try many Jquery codes and spent hours in front of computer. Could you please post some of your attempts? –  palaѕн Oct 4 '13 at 10:35
    
All of them are from stackoverflow. Please see the link. I have an error in consol also –  user2846122 Oct 4 '13 at 10:43

2 Answers 2

use each to loop throw your json data

<script type="text/javascript">
    $.each(yourData, function(key, val) {

           //your code here
           alert(key + '=' + val)

     });
 </script>
share|improve this answer
    
I am totally new to JS. Please post full code. Also see the update (Image Link) on my post. –  user2846122 Oct 4 '13 at 10:44
var html="<table border=1 style='border-collapse:collapse'>";

var json=[
 {"row1":"test","row2":"5.123456","row3":"6.123456"},
 {"row1":"test1","row2":"6.123456","row3":"8.123654"},
 {"row1":"test3","row2":"6.321456","row3":"8.964512"}
];
json.forEach(function(obj,index){html+="<tr><td>"+index+"<td>"+obj.row1+"</td>    <td>"+obj.row2+"</td><td>"+obj.row3+"</td></tr>";});
html+="</table>";
jQuery(html).appendTo("body");

Check out this fiddle http://jsfiddle.net/TdM8s/

share|improve this answer
    
Thanks friend, it's working. This is the code I need. Please post full code ie, ajax request to server + jquery parser (Your Code). Why I am getting error in FF console 'SyntaxError: JSON.parse: unexpected end of data' –  user2846122 Oct 4 '13 at 11:16
    
It seems that you have trouble getting data from server try using console.log(data) before using it. Also check out the following thread stackoverflow.com/questions/9321510/… –  melc Oct 4 '13 at 14:48

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.