您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

js读写json文件实例代码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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:BaiduYunMyHtmlDB_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:BaiduYunMyHtmlDB_USER.json", ForReading);       s = ts.ReadLine();       var json = eval('(' + s + ')');       alert(json.MyData[0].id); }catch(err){       }finally{ ts.Close(); } }