Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

javascript - Generate a CSV file from a JSON object

It's all in the title, I would like to generate a CSV file from the JSON objet get from this Ajax request,

The JSON I get represent all the recordings from a form :

enter image description here

I already have something that work for one field value a a single recording (the 0 here) :

    <!DOCTYPE html>
    <meta charset="utf-8"/>
    <html>
        <head>
            <title>This is Website Title</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
            <script src="../FileSaver.js"></script>
            <script>
                var formId = 566091
                // Définition des paramètres de la requête HTTP
                var settings = {
                    "async": true,
                    "crossDomain": true,
                    "url": "https://www.kizeoforms.com/rest/v3/forms/"+formId+"/data/readnew",
                    "method": "GET",
                    "headers": {
                            "content-type": "application/json",
                            "Authorization": "******",
                    }
                }

                // Envoi de la requête et affichage en console de la réponse
                $.ajax(settings).done(function (response) {
                    console.log(response);
                    var ssa3 = [];
                    for (var i = 0 ; i <= response.data.length; i++) {
                        ssa3.push(response.data[i].fields.ssa_3_a_22h00.value);
                    }
                    //var ssa3 = response.data[0].fields.ssa_3_a_22h00.value;
                    var blob = new Blob([ssa3], {type: "application/csv;charset=utf-8"});
                    saveAs(blob, "ssa3.csv");
                });
            </script>
        </head>
    </html>

I would now like to have this field value for all the recordings, I have tried to push it into a table but console tells me "i is undefined"

                $.ajax(settings).done(function (response) {
                    console.log(response);
                    var ssa3 = [];
                    for (var i = 0 ; i <= response.data.length; i++) {
                        ssa3.push(response.data[i].fields.ssa_3_a_22h00.value);
                    }
                    var blob = new Blob([ssa3], {type: "application/csv;charset=utf-8"});
                    saveAs(blob, "ssa3.csv");
                });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally found it, I used a forEach to browse the data :

$.ajax(settings).done(function (response) {

                console.log(response);
                var ronde1n = [];

                //on définit data qu'on va devoir parcourir avec un forEach
                const data = response.data;

                //on envoie les headers du fichiers csv
                ronde1n.push("Numéro d'enregistrement,ID,Date et heure,conso SSA3");

                //on parcours l'ensemble des enregistrements du formulaire Ronde 1 nuit
                  data.forEach(function (i) {
                     //on envoie les valeurs des champs qui nous intéressent pour chaque enregistrement
                     ronde1n.push("
" + i.record_number + "," + i.id + "," + i.fields.date_et_heure.value + "," + i.fields.ssa_3_a_22h00.value);
                  });

                //création du fichier CSV
                var blob = new Blob([ronde1n], {type: "application/csv;charset=utf-8"});
                  saveAs(blob, "ronde1_nuit.csv");

            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...