Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function convert_csv($csv, $json)
- {
- /*
- * Open files.
- */
- $csv_handle = fopen($csv, 'r');
- $json_handle = fopen($json, "w");
- /*
- * Get the table headers.
- */
- $headers = fgetcsv($csv_handle);
- /*
- * Write the array name.
- */
- fwrite($json_handle, ""LogData":[");
- $FirstRecord = true;
- while ($row = fgetcsv($csv_handle))
- {
- /*
- * Ensure no trailing comma after last record.
- */
- if (!$FirstRecord)
- fwrite($json_handle, ",");
- else
- $FirstRecord = false;
- /*
- * Create JSON record and write to file.
- */
- $complete = array_combine($headers, $row);
- fwrite($json_handle, json_encode($complete));
- }
- /*
- * Close the array
- */
- fwrite($json_handle, "]");
- /*
- * Close the files.
- */
- fclose($csv_handle);
- fclose($json_handle);
- }
- TimeStamp,Value
- 1390364805600.01,2.0
- 1390451205600.01,3.0
- 1390537605600.01,0.5
- 1390546245600.02,23.0
- 1390563525599.99,0.8
- "LogData":[{"TimeStamp":"1390364805600.01","Value":"2.0"},{"TimeStamp":"1390451205600.01","Value":"3.0"},{"TimeStamp":"1390537605600.01","Value":"0.5"},{"TimeStamp":"1390546245600.02","Value":"23.0"},{"TimeStamp":"1390563525599.99","Value":"0.8"}]
- $this->logging_model->convert_csv($path, "/tmp/log.json");
- //header('Content-Type: application/json'); // I have tried with and without this line
- readfile("/tmp/log.json"); // push it out
- $.ajax({
- url: URL,
- type: "GET",
- dataType: "json",
- success: function(data) { SaveData(PointId, data);},
- error: function (request, status, error) { alert(error);},
- async: false
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement