如何检查上传文件是否是JSON文件?

发布于 2025-02-14 01:30:09 字数 146 浏览 0 评论 0原文

我正在从用户中获取文件输入,我想检查所选文件是否是JSON文件。我该怎么做?

<input type = "file" onChange = {checkJSON}/>

还如何从JSON文件中检索数据。

I am taking a file input from a user and I want to check if the selected file is a JSON file. How can I do that?

<input type = "file" onChange = {checkJSON}/>

Also how do I retrieve data from the JSON file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2025-02-21 01:30:09

如果JSON.PARSE引发错误,则可能是无效的,因此您可以通过将数据输入字符串然后尝试解析该数据来检查其是否有效。

    try {  
      const json = JSON.parse(jsonStr);  
    } catch (e) {  
      console.log('invalid json');  
    }

If JSON.parse throws an error, its most likely invalid, therefore you can check if its valid by getting the data into a string and then trying to parse it.

    try {  
      const json = JSON.parse(jsonStr);  
    } catch (e) {  
      console.log('invalid json');  
    }
内心荒芜 2025-02-21 01:30:09

您可以检查文件扩展名是否为JSON,并且确保以这种方式确保所选文件有效json

function Validate() {
  fileName = document.querySelector('#myfile').value;
  extension = fileName.split('.').pop();
  if (extension != "json")
  {
   alert("Sorry, " + fileName + " is invalid, allowed extension is json!");
   return false;
  }
  
  var file = document.getElementById('myfile').files[0];
  var reader = new FileReader();
  reader.readAsText(file, 'UTF-8');
  reader.onload = function(evt) {
    var jsondata = evt.target.result;
    try {  
      const json = JSON.parse(jsondata);  
      document.getElementById('jsondata').innerHTML = jsondata;
    } catch (e) {  
      alert("Sorry, " + fileName + " is not valid JSON file!");
      return false;  
    }
  }
  return true;
};

html内容:

<script src="script2.js"></script>
File: <input type="file" id="myfile" onchange="Validate()"/><br /><br />
<div id="jsondata"></div>

您可以使用我的 Little Working live Demo on replotit (两种情况 - onsubmit onChange 演示)

You can check if file extension is json and be sure that selected file is valid JSON in that way:

function Validate() {
  fileName = document.querySelector('#myfile').value;
  extension = fileName.split('.').pop();
  if (extension != "json")
  {
   alert("Sorry, " + fileName + " is invalid, allowed extension is json!");
   return false;
  }
  
  var file = document.getElementById('myfile').files[0];
  var reader = new FileReader();
  reader.readAsText(file, 'UTF-8');
  reader.onload = function(evt) {
    var jsondata = evt.target.result;
    try {  
      const json = JSON.parse(jsondata);  
      document.getElementById('jsondata').innerHTML = jsondata;
    } catch (e) {  
      alert("Sorry, " + fileName + " is not valid JSON file!");
      return false;  
    }
  }
  return true;
};

Html content:

<script src="script2.js"></script>
File: <input type="file" id="myfile" onchange="Validate()"/><br /><br />
<div id="jsondata"></div>

You can play with my little working live demo on ReplIt (both cases - onsubmit and onchange demos)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文