ASP解析JSON格式数据方法

VBScript 是 ASP 服务端程序的常用语言,VBScript 解析 JSON是个问题.,自己写解析程序不太容易,碰到这问题, 第一个想到的就是 JScript 了。

注意,以下文件均以UTF-8的编码保存!

方法一(这是直接在 asp 里混用脚本):

  
<%  
    Dim json, obj  
    json = "{a:""aaa"", b:{ name:""bb"", value:""text"" }, c:[""item0"", ""item1"", ""item2""]}"  
    Set obj = parseJSON(json)  

    Response.Write "JSON原文为:
"       Response.Write json     Response.Write "
"     Response.Write "a=" & obj.a & "
"       Response.Write "b=" & obj.b.name & "
"       Response.Write "c.length=" & obj.c.length & "
"       Response.Write "c.get(0)=" & obj.c.get(0) & "
"   Set obj = Nothing %>

还有一个方法就是 使用 MS 的 脚本控件,也一样是使用了 JScript

方法二:

解析文件: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


<%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原文为:
"       Response.Write json     Response.Write "
"     Response.Write "a=" & obj.a & "
"       Response.Write "b=" & obj.b.name & "
"       Response.Write "c.length=" & obj.c.length & "
"       Response.Write "c.get(0)=" & obj.c.get(0) & "
"   Set obj = Nothing %>

方法一和方法二执行结果:

ASP解析JSON格式数据方法_第1张图片

上面的方法应该是最简洁的方法了。

方法三:

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

<%Session.CodePage="65001"%>
<%Response.CodePage="65001"%>
<%Response.Charset="UTF-8" %>
<%
Dim strTest
strTest = "{name:""张三丰"", age:24, email:[""[email protected]"",""[email protected]""], family:{parents:[""父亲"",""母亲""],toString:function(){return ""家庭成员"";}}}"
Response.Write "JSON原文:
" Response.Write ( strTest ) Dim objTest InitScriptControl Set objTest = getJSONObject( strTest ) %>
姓名:
<%=objTest.name%>

邮件地址(方法一):
<% For i=0 To objTest.email.length Response.Write ( sc4Json.Eval("jsonObject.email["&i&"]") & "
") Next %> 邮件地址(方法二):
<% Dim email For i=0 To objTest.email.length getJSArrayItem email,objTest.email,i Response.Write email & "
" Next %>
家庭信息:
<% Dim ai For i=0 To objTest.family.parents.length getJSArrayItem ai, objTest.family.parents, i Response.Write ai & "
" Next %> toString()函数:
<%=objTest.family.toString()%>
toString属性:
<%=objTest.family.toString%> <% Set objTest=nothing %>

方法三执行行结果:

ASP解析JSON格式数据方法_第2张图片

文件下载:http://download.csdn.net/download/xieyunc/9842375



转载于:https://www.cnblogs.com/xieyunc/p/9126491.html

你可能感兴趣的:(ASP解析JSON格式数据方法)