Home > Web Front-end > JS Tutorial > js read and write json file example code_javascript skills

js read and write json file example code_javascript skills

WBOY
Release: 2016-05-16 16:33:02
Original
1858 people have browsed it

本节为大家介绍下js如何读写json文件,代码很简练

function funSave() {
var id = $('#testText1')[0].value;
var name = $('#testText2')[0].value;
var str = '{mydata:[' + '{id:' + id + ',name:' + name + '}' + ']}';

str = "{MyData:[{id:'" + id + "',name:'" + name + "'}]}";

//var json = eval('(' + str + ')');

var fso, tf;
try{
fso = new ActiveXObject("Scripting.FileSystemObject");
      tf = fso.CreateTextFile("F:\\BaiduYun\\MyHtml\\DB_USER.json", true);
      tf.WriteLine(str);
}catch(err){


      }finally{
      tf.Close();
      }
}

function funSearch() {
var fso, ts, s;
var ForReading = 1;
try{
fso = new ActiveXObject("Scripting.FileSystemObject");
      ts = fso.OpenTextFile("F:\\BaiduYun\\MyHtml\\DB_USER.json", ForReading);
      s = ts.ReadLine();
      var json = eval('(' + s + ')');
      alert(json.MyData[0].id);
}catch(err){


}finally{
ts.Close();
}
} 
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template