如何检查上传文件是否是JSON文件?
我正在从用户中获取文件输入,我想检查所选文件是否是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果JSON.PARSE引发错误,则可能是无效的,因此您可以通过将数据输入字符串然后尝试解析该数据来检查其是否有效。
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.
您可以检查文件扩展名是否为JSON,并且确保以这种方式确保所选文件有效json :
html内容:
您可以使用我的 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:
Html content:
You can play with my little working live demo on ReplIt (both cases - onsubmit and onchange demos)