ASP解析JSON格式数据方法

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

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

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

<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 的 脚本控件,也一样是使用了 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

<!--#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
%>

方法三执行行结果:

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


 

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值