I have reviewed various resources, including Iterate through nested json object array, but none of them seem to address my specific situation.
The current challenge I am facing involves storing multiple JSON objects in an array within local storage. This is necessary in case the client's form goes offline and needs to store several submissions locally. Upon reconnecting, the goal is to iterate through all the stored data in local storage and submit each JSON object as a new row into an SQL database.
Currently, I have arrays of JSON objects being stored in a local storage array. However, I'm struggling to extract each individual "{...}" object separately. Instead, I can only retrieve a single value from the array string using offlinExtractData[3] (which returns "c" from "cable_no") rather than obtaining the first JSON Object in the array containing multiple items.
For instance, a complete JSON object that will be submitted to the SQL DB looks like this before being sent to a PHP SQL file via AJAX:
{"cable_no":"90012","section_no":"1","extract_ft":"1","cable_status":"","conduit_status":"None","extract_date":"2013-09-26","extraction_team":" ","extract_notes":"1"}
While that represents one row of data, here is a snapshot of what my local storage will resemble after numerous form submissions:
[{"cable_no":"90012","section_no":"1","extract_ft":"1","cable_status":"Done","conduit_status":"None","extract_date":"2013-09-26","extraction_team":" ","extract_notes":"1"},
{"cable_no":"90012","section_no":"1","extract_ft":"2","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"E1","extract_notes":"2"},
{"cable_no":"90012","section_no":"1","extract_ft":"3","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"3"},
{"cable_no":"90012","section_no":"4","extract_ft":"4","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"4"},
{"cable_no":"90012","section_no":"1.2.3","extract_ft":"333","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"5"}]
Each {...},{...},{...}
JSON Object has its unique row of data, and my objective is to return each {...}
separately for submission through an AJAX function already established. How can I iterate through each object to retrieve the entire dataset instead of fragments of strings? Does that clarify my query?
Below is the code responsible for generating the aforementioned data:
//Global variables
var extractform = {
'cable_no' : "",
'section_no' : "",
'extract_ft' : "",
'cable_status' : "",
'conduit_status' : "",
'extract_date' : "",
'extraction_team' : "",
'extract_notes' : ""
}
//initially set up the store
if (!localStorage["Extract_Update"] ) {
localStorage["Extract_Update"] = JSON.stringify([]);
}
//Now return locally stored values, if set
if (localStorage["Extract_Update"]) {
var offlinExtractData = localStorage["Extract_Update"];
console.log("Array Length: " + localStorage["Extract_Update"].length + "\nParsed Data: " + offlinExtractData);
console.log(offlinExtractData[3]);
}
function update_extractform() {
$.ajax({
type: 'POST',
//url: './php/update_extractform.php',
timeout: 8000, //8 seconds
data: extractform,
success: function(data) {
//Save data (array) and push new data into existing web storage
var aa = JSON.parse( localStorage["Extract_Update"] );
console.log( aa );
aa.push( [ extractform ] );
localStorage["Extract_Update"] = JSON.stringify( aa );
console.log("Submitted (offline): " + localStorage["Extract_Update"]);
}