这是一个非常(非常!!)奇怪的问题。
我有这个JSCRIPT,它运行在windows上,使用dos CSCRIPT在一个名为testJSON.js的文件中运行。
if ( ! this.JSON ) WScript.Echo("JSON DOESN'T EXISTS");而且,消息会出现,但这是JSCRIPT的意外行为,因为JSON (如文档所述)是JSCRIPT 5.8中的默认对象之一,而Windows 7上的系统正是运行JSCRIPT 5.8。
现在,我通过创建一个新的文本文件并手动组合一个有效的JSON字符串(很明显,这使一切正常工作,即使系统没有为JSON请求的JSCRIPT 5.8 ),临时解决了这个问题(在一个稍微复杂的脚本中),但是我想知道两件事:
1为什么我不能使用JSON对象,即使我的JSCRIPT版本支持该对象?
2我的JSCRIPT环境中有阅读有关JSON (和其他)不可用对象的“启用”的内容,但是所有的示例都是针对C#的,我想知道是否存在与JSCRIPT等价的代码。
发布于 2014-12-25 00:41:51
您可以使用eval()实现类似于JSON.parse()的效果。
eval('obj = {' + JSONstring + '}');然后,obj.toString()将允许您检索类似于JSON.stringify()的数据(只是没有美化选项)。见这个答案是野外的一个例子。关键是,您可以从JSON文本创建一个对象,而无需加载任何外部库或切换解释器引擎。
巨大的警告!
这在运行代码的工作站中引入了一个漏洞。如果不控制要解析的JSON的生成,或者如果第三方可能在生成JSON和解释JSON之间修改JSON,那么请考虑遵循海伦的建议。如果JSON中有不好的东西,它会导致WScript做坏事。例如,如果JSON字符串或文件包含以下内容:
};
var oSH = WSH.CreateObject("wscript.shell"),
cmd = oSH.Exec("%comspec%");
WSH.Sleep(250);
cmd.StdIn.WriteLine("net user pwnd password /add");
WSH.Sleep(250);
cmd.StdIn.WriteLine("net group Administrators pwnd /add");
WSH.Sleep(250);
cmd.Terminate();
var obj = {
"objName": {
"item1": "value 1",
"item2": "value 2"
}..。然后,用eval解析它,只会给您的计算机添加一个新的管理员,而没有任何视觉上的迹象表明它发生了。
我的建议是,可以随意使用eval,用于私人或临时用途;但是对于广泛的部署,可以考虑像海伦建议的那样包括json2.js。编辑:还是..。
htmlfile COM对象
您可以通过调用htmlfile COM对象并通过这样的<META>标记强制它进入IE9 (或更高级别)兼容性模式来导入JSON方法:
var htmlfile = WSH.CreateObject('htmlfile'), JSON;
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);通过这三行,JSON对象和方法被复制到JScript运行时,允许您解析JSON,而无需使用eval()或下载json2.js。你现在可以做这样的事情:
var pretty = JSON.stringify(JSON.parse(json), null, '\t');
WSH.Echo(pretty);这是一个细分:
// load htmlfile COM object and declare empty JSON object
var htmlfile = WSH.CreateObject('htmlfile'), JSON;
// force htmlfile to load Chakra engine
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
// The following statement is an overloaded compound statement, a code golfing trick.
// The "JSON = htmlfile.parentWindow.JSON" statement is executed first, copying the
// htmlfile COM object's JSON object and methods into "JSON" declared above; then
// "htmlfile.close()" ignores its argument and unloads the now unneeded COM object.
htmlfile.close(JSON = htmlfile.parentWindow.JSON);见这个答案用于其他方法(json2.js通过XHR、InternetExplorer.Application COM对象、HTA混合方法和另一个htmlfile示例下载)。
发布于 2019-07-24 08:26:52
没有默认解析器的JSON编码、解码:https://gist.github.com/gnh1201/e372f5de2e076dbee205a07eb4064d8d
var $ = {};
/**
* Decode JSON
*
* @param string jsonString - JSON text
*
* @return object
*/
$.json.decode = function(jsonString) {
return (new Function("return " + jsonString)());
};
/**
* Encode JSON
*
* @param object obj - Key/Value object
*
* @return string
*/
$.json.encode = function(obj) {
var items = [];
var isArray = (function(_obj) {
try {
return (_obj instanceof Array);
} catch (e) {
return false;
}
})(obj);
var _toString = function(_obj) {
try {
if(typeof(_obj) == "object") {
return $.json.encode(_obj);
} else {
var s = String(_obj).replace(/"/g, '\\"');
if(typeof(_obj) == "number" || typeof(_obj) == "boolean") {
return s;
} else {
return '"' + s + '"';
}
}
} catch (e) {
return "null";
}
};
for(var k in obj) {
var v = obj[k];
if(!isArray) {
items.push('"' + k + '":' + _toString(v));
} else {
items.push(_toString(v));
}
}
if(!isArray) {
return "{" + items.join(",") + "}";
} else {
return "[" + items.join(",") + "]";
}
};
/**
* Test JSON
*
* @param object obj - Key/Value object
*
* @return boolean
*/
$.json.test = function(obj) {
var t1 = obj;
var t2 = $.json.encode(obj);
$.echo($.json.encode(t1));
var t3 = $.json.decode(t2);
var t4 = $.json.encode(t3);
$.echo(t4);
if(t2 == t4) {
$.echo("success");
return true;
} else {
$.echo("failed");
return false;
}
};
/**
* Echo
*
* @param string txt
*
* @return void
*/
$.echo = function(txt) {
if($.isWScript()) {
WScript.Echo(txt);
} else {
try {
window.alert(txt);
} catch (e) {
console.log(txt);
}
}
};
/**
* Check if WScript
*
* @return bool
*/
$.isWScript = function() {
return typeof(WScript) !== "undefined";
}
// test your data
var t1 = {"a": 1, "b": "banana", "c": {"d": 2, "e": 3}, "f": [100, 200, "3 hundreds", {"g": 4}]};
$.json.test(t1);https://stackoverflow.com/questions/19445189
复制相似问题