例子,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 + ')');
//www.jb200.com
var fso, tf;
try{
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("F:BaiduYunMyHtmlDB_USER.json", true);
tf.WriteLine(str);
}catch(err){
}finally{
tf.Close();
}
}
//读取json数据
function funSearch() {
var fso, ts, s;
var ForReading = 1;
try{
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("F:BaiduYunMyHtmlDB_USER.json", ForReading);
s = ts.ReadLine();
var json = eval('(' + s + ')');
alert(json.MyData[0].id);
}catch(err){
}finally{
ts.Close();
}
}