nj51 nj51 - 1 year ago 94
jQuery Question

ajax JSON call returning array of strings instead of objects

When I do the ajax call I am parsing the json_encoded data and when I log the data to the console it's actually an array of strings instead of objects. It's showing this.

"{" todoText":"dgdgdfgdfgdf",

"todoText":"test 2",

"todoText":"test 3",


This is the code I used to make the call to retrieve the data.

$(document).ready(function() {

$.get("php/listtasks.php", function(data){

var parsed = JSON.parse(data);




This is the php code i used to encode the data and echo it back to the javascript.

$query = "SELECT * FROM list";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);

$rows = $result->num_rows;

for ($j = 0 ; $j < $rows ; ++$j)
$row = $result->fetch_array(MYSQLI_NUM);

$x[$j] = $row[2];


echo json_encode($x);

Answer Source

Apparently, your $row[2] is a JSON object so you need to decode it like this:

$x[$j] = json_decode($row[2]);

I hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download