VBScript 是 ASP 服务端程序的常用语言,VBScript 解析 JSON是个问题,自己写解析程序不太容易,碰到这问题, 第一个想到的就是 JScript 了。注意,以下文件均以UTF-8的编码保存!
方法一(这是直接在 asp 里混用JS脚本):
<script language="jscript" runat="server"> Array.prototype.get = function(x) { return this[x]; }; function parseJSON(strJSON) { return eval("(" + strJSON + ")"); } </script> <% Dim json, obj json = "{a:""aaa"", b:{ name:""bb"", value:""text"" }, c:[""item0"", ""item1"", ""item2""]}" Set obj = parseJSON(json) Response.Write "JSON原文为:<br>" Response.Write json Response.Write "<hr>" Response.Write "a=" & obj.a & "<br />" Response.Write "b=" & obj.b.name & "<br />" Response.Write "c.length=" & obj.c.length & "<br />" Response.Write "c.get(0)=" & obj.c.get(0) & "<br />" Set obj = Nothing %>
还有一个方法就是 使用 MS 的 脚本控件,也一样是使用了 JS。
方法二:
解析文件:paseJSON.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <%Session.CodePage="65001"%> <%Response.CodePage="65001"%> <%Response.Charset="UTF-8" %> <% Dim scriptCtrl Function parseJSON(str) If Not IsObject(scriptCtrl) Then Set scriptCtrl = Server.CreateObject("MSScriptControl.ScriptControl") scriptCtrl.Language = "JScript" scriptCtrl.AddCode "Array.prototype.get = function(x) { return this[x]; }; var result = null;" End If scriptCtrl.ExecuteStatement "result = " & str & ";" Set parseJSON = scriptCtrl.CodeObject.result End Function %>
测试文件:c.asp
<!--#include file="jsonParse.asp"--> <%Session.CodePage="65001"%> <%Response.CodePage="65001"%> <%Response.Charset="UTF-8" %> <% Dim json json = "{a:""aaa"", b:{ name:""bb"", value:""text"" }, c:[""item0"", ""item1"", ""item2""]}" Set obj = parseJSON(json) Response.Write "JSON原文为:<br>" Response.Write json Response.Write "<hr>" Response.Write "a=" & obj.a & "<br />" Response.Write "b=" & obj.b.name & "<br />" Response.Write "c.length=" & obj.c.length & "<br />" Response.Write "c.get(0)=" & obj.c.get(0) & "<br />" Set obj = Nothing %>
方法一和方法二执行结果:
上面的方法应该是最简洁的方法了。
方法三:
JSON解析文件:jsonParse.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <%Session.CodePage="65001"%> <%Response.CodePage="65001"%> <%Response.Charset="UTF-8" %> <% 'Option Explicit Dim sc4Json Sub InitScriptControl Set sc4Json = Server.CreateObject("MSScriptControl.ScriptControl") sc4Json.Language = "JavaScript" sc4Json.AddCode "var itemTemp=null;function getJSArray(arr, index){itemTemp=arr[index];}" End Sub Function getJSONObject(strJSON) sc4Json.AddCode "var jsonObject = " & strJSON Set getJSONObject = sc4Json.CodeObject.jsonObject End Function Sub getJSArrayItem(objDest,objJSArray,index) On Error Resume Next sc4Json.Run "getJSArray",objJSArray, index Set objDest = sc4Json.CodeObject.itemTemp If Err.number=0 Then Exit Sub objDest = sc4Json.CodeObject.itemTemp End Sub %>
测试文件:test.asp
<!--#include file="jsonParse.asp"--> <%Session.CodePage="65001"%> <%Response.CodePage="65001"%> <%Response.Charset="UTF-8" %> <% Dim strTest strTest = "{name:""张三丰"", age:24, email:[""zsf@163.com"",""zsf@gmail.com""], family:{parents:[""父亲"",""母亲""],toString:function(){return ""家庭成员"";}}}" Response.Write "JSON原文:<br>" Response.Write ( strTest ) Dim objTest InitScriptControl Set objTest = getJSONObject( strTest ) %> <hr> 姓名:<br> <%=objTest.name%><br> <hr> 邮件地址(方法一):<br> <% For i=0 To objTest.email.length Response.Write ( sc4Json.Eval("jsonObject.email["&i&"]") & "<br>") Next %> 邮件地址(方法二):<br> <% Dim email For i=0 To objTest.email.length getJSArrayItem email,objTest.email,i Response.Write email & "<br>" Next %> <hr> 家庭信息:<br> <% Dim ai For i=0 To objTest.family.parents.length getJSArrayItem ai, objTest.family.parents, i Response.Write ai & "<br>" Next %> toString()函数:<br> <%=objTest.family.toString()%> <br> toString属性:<br> <%=objTest.family.toString%> <% Set objTest=nothing %>
方法三执行结果:
· END ·