开发者

Writing json string into file using Windows Script Host

I am trying to write json string that I stringified using https://github.com/douglascrockford/JSON-js/blob/master/json2.js ( JSON.stringify) ,

I am trying to save the string and I am getting an error:

Microsoft JScript Runtime Error: Invalid procedure call or argument

at

var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse); 

this is my code :

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();


The constants you pass to the FSO methods are not exposed via late binding with new ActiveXObject so you need to define them independently;

var ForWriting = 2;
var TristateFalse = 0;

Edit

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜