JSON created with PHP pass to JAVASCRIPT

1

PHP le a CSV file and generates a JSON

require __DIR__ . '\autoload.php';
use League\Csv\Reader;
$arq = __DIR__ . '\arquivos1111019050.csv';

$reader = Reader::createFromPath($arq, 'r');
$records = $reader->getRecords();
$tmp = new SplTempFileObject();
foreach ($records as $offset => $record) {
    $tmp->fputcsv($record);
}

$reader = Reader::createFromFileObject($tmp);
$reader->setDelimiter(';');
$reader->setHeaderOffset(0);
$reader = json_encode($reader);
?>

Download JSON in PHP for JAVASCRIPT

<script>
    // ACHO QUE MEU ERRO ESTA AQUI 
    var arrayOfObjects = "<?="$reader"?>";
    // -----------------------------------
    for (var i = 0; i < arrayOfObjects.length; i++) {
        var object = arrayOfObjects[i];
        for (var property in object) {
            alert('item ' + i + ': ' + property + '=' + object[property]);
        }

    }
</script>

CVS file - 201111019050.csv

[
{"sistema":"xxxx","dt_ini":"01:16","dt_fim":"03:20","dt_esti":"03:13","job":"xxxxx"},
{"sistema":"aaaaa","dt_ini":"01:42","dt_fim":"03:46","dt_esti":"03:47","job":"aaaaa"},
{"sistema":"bbbbb","dt_ini":"01:17","dt_fim":"03:21","dt_esti":"03:25","job":"bbbbb"},
{"sistema":"ccccc","dt_ini":"02:28","dt_fim":"04:32","dt_esti":"04:42","job":"ccccc"},
]
    
asked by anonymous 08.11.2017 / 18:48

2 answers

0

You have extra quotes and you should use ; in PHP. Then you have to parse that string into a JSON. You can do this in JavaScript with JSON.parse

Changes:

var arrayOfObjects = "<?="$reader"?>";

for

var json = "<?=$reader;?>";
var arrayOfObjects = JSON.parse(json);
    
08.11.2017 / 19:22
0

HTML generated by the page !!!!!

            <!DOCTYPE html>
            <html>
            <body>

                <p id="nome"></p>
                <script>
                    var x;
                    var json = "[{"sistema":"SIART","dt_ini":"01:16","dt_fim":"03:20","dt_esti":"03:13","job":"ARTABERT"},{"sistema":"SIACC","dt_ini":"01:42","dt_fim":"03:46","dt_esti":"03:47","job":"ACC99RUL"},{"sistema":"SICEX","dt_ini":"01:17","dt_fim":"03:21","dt_esti":"03:25","job":"CEX05CPN"},{"sistema":"SICES","dt_ini":"02:28","dt_fim":"04:32","dt_esti":"04:42","job":"CES00MZB"},{"sistema":"SINCE","dt_ini":"03:38","dt_fim":"05:42","dt_esti":"05:17","job":"NCE32MZA"},{"sistema":"SICOV","dt_ini":"00:34","dt_fim":"02:38","dt_esti":"","job":"COV06MZC"},{"sistema":"SIADC","dt_ini":"03:52","dt_fim":"05:56","dt_esti":"06:00","job":"ADCABERT"},{"sistema":"SIGSJ","dt_ini":"05:55","dt_fim":"07:59","dt_esti":"","job":"GSJ01SLA"},{"sistema":"GOV_CONTA","dt_ini":"03:20","dt_fim":"05:24","dt_esti":"05:00","job":"GOV04MZB"},{"sistema":"SID00","dt_ini":"01:35","dt_fim":"03:39","dt_esti":"02:40","job":"D0002MZE"},{"sistema":"SIADT","dt_ini":"00:42","dt_fim":"02:46","dt_esti":"02:47","job":"ADTABRE2"},{"sistema":"SIBAN_CONSTRUCARD","dt_ini":"05:24","dt_fim":"07:28","dt_esti":"","job":"BAN00MZC"},{"sistema":"SIDEM","dt_ini":"06:23","dt_fim":"08:27","dt_esti":"","job":"DEM00SLA"},{"sistema":"B24_FUNCOES_INTERNAS","dt_ini":"05:27","dt_fim":"07:31","dt_esti":"","job":"B2400MZB"},{"sistema":"B24_CADASTRO_GERAL_C999","dt_ini":"01:01","dt_fim":"03:05","dt_esti":"03:03","job":"B2461MZB"},{"sistema":"B24_CADASTRO_POUPANCA_013","dt_ini":"00:01","dt_fim":"02:05","dt_esti":"","job":"B2461MZD"},{"sistema":"B24_CADASTROS_COMPLEMENTARES","dt_ini":"01:09","dt_fim":"03:13","dt_esti":"03:10","job":"B2461MZJ"},{"sistema":"B24_GERACAO_POUPANCA_COMPLEMENTAR","dt_ini":"01:18","dt_fim":"03:22","dt_esti":"03:17","job":"B2461MZL"}]";
                    var arrayOfObjects = JSON.parse(json);


                    for (x in arrayOfObjects) {
                        document.getElementById("nome").innerHTML += x +="</BR>";
                    } 
                </script>

            </body>
            </html>
    
13.11.2017 / 16:19