Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - Save JSON data to text file and read it

Is there possible to save JSON data into local text file? So later i can use it again using by load that file and get the stored JSON data back. Actually want i really want to do is to export JSON data in text file so i can use later as import.Any suggestion or solution here?

This is some example that i want to use to export to text.

http://jsfiddle.net/k56eezxp/

var data = new Blob([text], {type: 'text/plain'});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is there possible to save JSON data into local text file?

Yes. Currently JavaScript at linked jsfiddle creates a .txt file, not a valid JSON file.

You can use try..catch..finally and JSON.parse() to check if input at <textarea> element is valid JSON. If .value of <textarea> is valid JSON create Blob URL using Blob or File constructor with MIME type property set to "application/json". and URL.createObjectURL(), else notify user that input is invalid JSON.

(function () {

 let file, url, reader = new FileReader;
 
 function createJSONFile(json) {
    let e = void 0;
    try {
      JSON.parse(json)
    } catch (err) {
      e = err;
      code.textContent = e;
    }
    finally {
      if (e) {
        code.classList.add("invalid");
        return "Invalid JSON";
      }
      else {
        code.classList.remove("invalid");
        file = new File([json], "info.json", {type:"application/json"});
        url = URL.createObjectURL(file);
        return url;
      }
    }
  };
  
  function revokeBlobURL() {
    window.removeEventListener("focus", revokeBlobURL);
    URL.revokeObjectURL(url);
    if (file.close) {
      file.close();
    }    
  }
  
  function readJSON(e) {
    reader.readAsText(input.files[0]);
  }
 
  let create = document.getElementById("create"),
    textbox = document.getElementById("textbox"),
    code = document.querySelector("code"),
    input = document.querySelector("input[type=file]"),
    pre = document.querySelector("pre");

  create.addEventListener("click", function () {
    var link = document.createElement("a");
    link.setAttribute("download", "info.json");
    var json = createJSONFile(textbox.value);
    if (json !== "Invalid JSON") {
      link.href = json;
      document.body.appendChild(link);
      code.textContent = "Valid JSON";
      link.click();
      window.addEventListener("focus", revokeBlobURL);
    } else {
      code.textContext = json;
    }
  }, false);
  
  reader.addEventListener("load", function() {
    pre.textContent = JSON.stringify(reader.result, null, 2);
  });
  
  input.addEventListener("change", readJSON);
})();
code {
  display:block;
  width: 350px;
  height: 28px;
  border: 1px dotted green;
  padding: 4px;
  margin: 4px;
  color: green;
}

.invalid {
  border: 1px dotted red;
  color: red;
}

pre {
  background: #eee;
  width: 350px;
  height: 350px;
  border: 1px solid darkorange;
}
<textarea id="textbox" placeholder="Input valid JSON"></textarea><br>
<button id="create">Create file</button> 
<br>
<code></code>
<input type="file" accept=".json" />
<pre></pre>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...