<%
Dim scriptCtrl ‘这个dim很重要一定要加,如果不加会输出不正常,切记
Function parseJSON(str)
If Not IsObject(scriptCtrl) Then
Set scriptCtrl = Server.CreateObject("MSScriptControl.ScriptControl")
scriptCtrl.Language = "JScript"
scriptCtrl.AddCode "function ActiveXObject() {}" ′ 覆盖 ActiveXObject
scriptCtrl.AddCode "function GetObject() {}" ′ 覆盖 ActiveXObject
scriptCtrl.AddCode "Array.prototype.get = function(x) { return this[x];}; var result = null;"
End If
On Error Resume Next
scriptCtrl.ExecuteStatement "result = " & str & ";"
Set parseJSON = scriptCtrl.CodeObject.result
If Err Then
Err.Clear
Set parseJSON = Nothing
End If
End Function
Dim json
json = "{a:""a的值"", b:{ name:""b的值"", value:""text"" }, c:[""C1的值"", ""C2的值"", ""C3的值""]}"
Set obj = parseJSON(json)
Response.Write obj.a & "<br />"
Response.Write obj.b.name & "<br />"
Response.Write "c有"&obj.c.length&"个值" & "<br />"
for i=0 to obj.c.length-1
Response.Write obj.c.get(i) & "<br />"
next
%>
输出的结果是:
a的值
b的值
c有3个值
C1的值
C2的值
C3的值