I've found numerous questions pertaining to this subject with very little votes, but I can't seem to get any of the answers to work thus far. I've made a PHP page on a web server into a application/json that turns a mysqli query into an array and encodes it using json_encode so that it is now a JSON object. I am now trying to decode the JSON object using javascript, but the only solutions I can find deal with Arrays and not Objects. Ultimately, I'd like to decode the JSON and iterate through it so that I can INSERT the data into a Sqlite database on the client. I'm having difficulty getting any results on the client to work except being able to Stringify the JSon so that I can see that it has been retrieved. My code is as follows:
Web server retrieveJSON.php page
<?php header('Content-Type: application/json');
$mysqli= new mysqli("host","user","password","database");
mysqli_select_db($mysqli,"database");
$query = "SELECT * FROM AppCustomers";
$result = mysqli_query($mysqli,$query) or die('Errant query: '.$query);
$customers = array();
if(mysqli_num_rows($result)) {
while($customer = mysqli_fetch_assoc($result)) {
$customers[] = array('customer'=>$customer);
}
}
$json_array = array('customers'=>$customers,);
echo json_encode($json_array);
mysqli_close($mysqli);
?>
Client side Javascript
<script>
$.ajax({
url : 'http://webserver/retrieveJSON.php',
dataType : 'json',
type : 'get',
success : function(Result){
//ResultAlert = JSON.stringify(Result);
//alert(ResultAlert);
}
});
</script>
When I stringify the result I get a long version of the following JSON object:
{"customers":[{"customer":{"id":"1","customerName":"Customer Alpha","customerID":" custA","customerAddress":" Alpha Way","customerCity":" Alpha","customerState":" AL","customerZip":"91605"}},{"customer":{"id":"2","customerName":"Customer Beta","customerID":" CustB","customerAddress":" Beta Street","customerCity":" Beta","customerState":" BE","customerZip":"91605"}}]}
I have a database and the following insert function already set up.
function insertCustomer(customerName, customerID, customerAddress, customerCity, customerState, customerZip) {
db.transaction(function (tx) {
tx.executeSql('INSERT INTO Customers (customerName, customerID, customerAddress, customerCity, customerState, customerZip) VALUES (?, ?, ?, ?, ?, ?)', [customerName, customerID, customerAddress, customerCity, customerState, customerZip],CountReturns);
});
};
How do I turn the object back into an Array so that I can iterate through it and insert each field into my Sqlite database? In other words what do I replace the //stringify portion with? I hope this question is not too localized, but I tried to lay out the whole process in case anyone else is trying to do the same kind of thing. Any other tips or suggestions in the process are welcomed as well. Thanks.
Thanks to Quentin I was able to access the array and input the fields into the database using the following code in place of the stringify; however, all the inputs were undefined for some reason.
for (var i = 0, len = Result.customers.length; i < len; ++i) {
var customer = Result.customers[i];
insertCustomer(customer.customerName, customer.customerID, customer.customerAddress, customer.customerCity, customer.customerState, customer.customerZip);
}
JSON.parse
. – Paul S. May 3 at 16:32