我正在尝试编写使用https://github.com/douglascrockford/JSON-js/blob/master/json2.js(JSON.stringify)进行字符串化的json字符串,
我正在尝试保存字符串,但我收到错误:
Microsoft JScript运行时错误:无效的过程调用或参数
在
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse);
这是我的代码:
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref);
WSH.echo(textPref)
// Create the new file.
fso.CreateTextFile("d:\\Preferences_temp", true);
var fileObj = fso.GetFile("d:\\Preferences_temp");
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse); <- Microsoft JScript Runtime Error: Invalid procedure call or argument
textStream.Write(textPref);
textStream.Close();
答案 0 :(得分:3)
传递给FSO方法的常量不会通过与new ActiveXObject
的后期绑定公开,因此您需要单独定义它们;
var ForWriting = 2;
var TristateFalse = 0;
修改强>
var ForWriting = 2;
var TristateTrue = -1;
var textPref = '{"xxx": "AA \u05D5 BB"}';
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref)
// Create the new file.
var fso = new ActiveXObject("Scripting.FileSystemObject");
//create as unicode
fso.CreateTextFile("c:\\null\\Preferences_temp", true, true);
var fileObj = fso.GetFile("c:\\null\\Preferences_temp");
//open for unicode
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateTrue);
textStream.Write(textPref);
textStream.Close();