代码天地

asp解析json复杂结构案列分享
2019.08.14 代码天地

在做asp解析json的时候,有时候返回的json会很复杂,此时我们该怎么用asp解析json呢,接下来我们就来通过一个案列分享asp解析复杂json,
包括两个文件:
JSON解析文件----jsonParse.asp
演示文件-------demo.asp
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 %>

演示文件demo.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 %>

最后输出结果如下:

asp解析json结果.png

上一篇下一篇
标签

有问
必答