首页 > 编程 > JavaScript > 正文

js读写json文件实例代码

2019-11-20 14:00:23
字体:
来源:转载
供稿:网友

本节为大家介绍下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();}} 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表