使用javascript读取和写入json文件
可能重复:使用javascript读取和写入json文件,javascript,html,json,Javascript,Html,Json,可能重复: 有人能提供使用javascript读写文件的示例代码吗 目前,我正在尝试从json文件中读取输入,并将其显示在文本框中,从而为用户提供编辑数据的灵活性。编辑的数据必须写入json文件。浏览器中显示的网页中运行的JavaScript无法访问客户端文件系统 但您可以使用API的(不使用javascript进行文件编程) 如果您的意思是在javascript中解析json,那么:- 您可以使用Douglas crockford JSON库进行解析:- JSON.parse方法 参考链接:
有人能提供使用javascript读写文件的示例代码吗
目前,我正在尝试从json文件中读取输入,并将其显示在文本框中,从而为用户提供编辑数据的灵活性。编辑的数据必须写入json文件。浏览器中显示的网页中运行的JavaScript无法访问客户端文件系统 但您可以使用API的(不使用javascript进行文件编程) 如果您的意思是在javascript中解析json,那么:-
var abcd= "[{"name" : "sandeep"},{"name" :"Ramesh"}]"
abcd =JSON.parse(abcd);
for (var index=0;index<abcd.length;index++){
alert(abcd[i].name);
}
var abcd=“[{”name:“sandeep”},{”name:“Ramesh”}”
abcd=JSON.parse(abcd);
对于(var index=0;index,这里是示例html文件,我已经用firefox对其进行了测试
<!DOCTYPE html>
<html>
<head>
<script>
function handleFileSelect()
{
if (window.File && window.FileReader && window.FileList && window.Blob) {
} else {
alert('The File APIs are not fully supported in this browser.');
return;
}
input = document.getElementById('fileinput');
if (!input) {
alert("Um, couldn't find the fileinput element.");
}
else if (!input.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
file = input.files[0];
fr = new FileReader();
fr.onload = receivedText;
fr.readAsText(file);
}
}
function receivedText() {
//result = fr.result;
document.getElementById('editor').appendChild(document.createTextNode(fr.result))
}
</script>
</head>
<body>
<input type="file" id="fileinput"/>
<input type='button' id='btnLoad' value='Load' onclick='handleFileSelect();'>
<div id="editor"></div>
</body>
</html>
函数handleFileSelect()
{
if(window.File&&window.FileReader&&window.FileList&&window.Blob){
}否则{
警报('此浏览器不完全支持文件API');
返回;
}
输入=document.getElementById('fileinput');
如果(!输入){
警报(“嗯,找不到fileinput元素。”);
}
如果(!input.files){
警报(“此浏览器似乎不支持文件输入的`files`属性。”);
}
如果(!input.files[0]),则为else{
警报(“请在单击“加载”之前选择一个文件”);
}
否则{
file=input.files[0];
fr=新文件读取器();
fr.onload=接收到的文本;
fr.readAsText(文件);
}
}
函数receivedText(){
//结果=fr.result;
document.getElementById('editor').appendChild(document.createTextNode(fr.result))
}
这篇文章应该为你提供最好的答案:@user1631651查看我下面的答案,这是一个工作示例..如果我在IIS中部署html页面并使用url访问文件是否可能..如果可以,请提供相同的示例代码..@user1631651查看我上面的答案,这是一个工作示例..我正在寻找一个可以访问一个文件..在本地文件系统中或通过url..请提供帮助。然后,如果您的文件在服务器中,则通过jquery$.post()进行ajax调用,并在回调中进行解析,然后对其进行操作。之后,使用另一个ajax调用将其发送到服务器,并将其保存在服务器中所需的位置。这不显示如何编写JSON