Javascript 使用javascript读取和写入json文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12259590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading and writing json file using javascript
提问by user1631651
Possible Duplicate:
How to read and write into file using JavaScript
可能的重复:
如何使用 JavaScript 读取和写入文件
can anybody provide sample code to read and write into file using javascript?
任何人都可以提供示例代码来使用 javascript 读取和写入文件吗?
at present i am trying to read input from json file and display it in textboxes providing the user flexibility to edit the data. Edited data has to be written into json file.
目前我正在尝试从 json 文件中读取输入并将其显示在文本框中,为用户提供编辑数据的灵活性。编辑后的数据必须写入 json 文件。
回答by Sark
here is the sample html file, i have tested it with firefox working fine.
这是示例 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>
回答by Peru
JavaScript running in a web page displayed in a browser cannot access the client file system.
在浏览器中显示的网页中运行的 JavaScript 无法访问客户端文件系统。
But you can use API's
但是你可以使用 API 的
回答by sandeep patel
(No File programming in javascript) If you mean parsing json in javascript then :-
(在 javascript 中没有文件编程)如果你的意思是在 javascript 中解析 json 那么:-
- you can use Douglas crockford JSON lib for parsing:- JSON.parse method Refer Link :- http://www.json.org/js.html
- 您可以使用 Douglas crockford JSON lib 进行解析:- JSON.parse 方法参考链接:- http://www.json.org/js.html
Example,
例子,
var abcd= "[{"name" : "sandeep"},{"name" :"Ramesh"}]"
abcd =JSON.parse(abcd);
for (var index=0;index<abcd.length;index++){
alert(abcd[i].name);
}