如何使用js上传和读取json文件?

时间:2020-06-16 02:12:47

标签: javascript json parsing filereader reader

我正在研究一个分析json文件并提供统计信息的网页。用户将必须提供json文件,但我想添加一个“示例”按钮来代替示例文件。 这是我想要做的:

*这不是完整的代码

$("#sample").on('click', function() {
    sampleButton = true;
    $.getJSON("static/samples/sample.json", function(data) {
      sendFile();
    });
});


function sendFile() {
    var reader = new FileReader();
    if (sampleButton == true){

    } else {
        reader.onload = onReaderLoad;
        reader.readAsText(document.getElementById('file_input').files[0]);
    }

    function onReaderLoad(event) {
        if (sampleButton == true){
            *** Read the sample.json file and store it in data***

        } else {
            var data = JSON.stringify(event.target.result);
            console.log(data);
            var data = JSON.parse(data);
        }

        $.post('/analyze', {
                data: data,
            }).done(function (response) {

请帮助!

0 个答案:

没有答案