Reading and writing json file using javascript

Possible duplicate:
How to read and write to a file using JavaScript

can anyone provide some sample code to read and write to a file using javascript?

I'm currently trying to read input from a json file and display it in text windows, giving the user the flexibility to edit data. Edited data should be written to json file.

+5
source share
3 answers

here is an example html file, I tested it working fine.

<!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>
+1
source

JavaScript running on a web page displayed in a browser cannot access the client file system.

But you can use the API

0

( javascript) json javascript, :

Example

var abcd= "[{"name" : "sandeep"},{"name" :"Ramesh"}]"

abcd =JSON.parse(abcd);

for (var index=0;index<abcd.length;index++){

alert(abcd[i].name);
}
0
source

All Articles