使用示例:
<!--#include file="json.inc"--> <%dim json,json1,json2,json3,jsonStrset json=new Aien_Json '定义主json对象json.JsonType="object" 'json数据结构为对象json.addData "name","panzhiming" '添加数据json.addData "isboy",truejson.addData "age",23json.addData "luckyNumber",Array(34,31,42)set json2=new Aien_Json '定义一个json子对象json2.JsonType="object" '数据结构为对象json2.addData "wife","xiaoming" '添加数据json2.addData "school","miliangxiaoxue" set json3=new Aien_Json '定义一个json子对象json3.JsonType="object" '数据结构为对象json3.addData "chinese",85 '添加数据json3.addData "english",90 set json1=new Aien_Json '定义一个json子对象json1.JsonType="array" '数据结构为数组json1.addData "color1","green" '添加数据json1.addData "color2","red"json2.addData "classes",json3 '把json3添加到json2中,键名为classes json.addData "colors",json1 '把json1添加到json中,键名为colorsjson.addData "others",json2 '把json2添加到json中,键名为othersjsonStr=json.getJson(json) '获取最后生成的json字符串,本函数只有第一次调用有效set json3=nothingset json2=nothingset json1=nothingset json=nothing'//输入生成数据Response.write jsonStr' jsonStr={ ' name:"panzhiming", ' isboy:true, ' age:23, ' luckyNumber:[34,31,42], ' colors:["green","red"], ' others:{ ' wife:"xiaoming", ' school:"miliangxiaoxue", ' classes:{chinese:85,english:90} ' }' }%>
类文件josn.inc:
<% '艾恩Asp生成Json数据类 '除jsEncode函数为json官方类函数外,其他内容均原创,转载请注明出处 'http://www.ii-home.cn QQ:1034555083 Email:zhanghuiguoanlige@126.com class Aien_Json private mCollection,mJsonType Public Property Get Collection() set Collection = mCollection End Property Public Property Let JsonType(mData) if lcase(mData)="object" then mJsonType = true elseif lcase(mData)="array" then mJsonType = false end if End Property Public Property Get JsonType() JsonType=mJsonType End Property Private Sub Class_Initialize() set mCollection=server.createobject("scripting.dictionary") End Sub Private Sub Class_Terminate() mCollection.removeAll() set mCollection=nothing End Sub public function addData(vars,values) if mCollection.exists(vars) then mCollection(vars)=mCollection(vars) & "," & values else mCollection.add vars,values end if end function Private function toJson(vars) dim values if lcase(typename(vars))="aien_json" then set values=vars else values=vars end if dim aryStr dim vType:vType=lcase(typename(values)) select case vType case "string" toJson="""" & jsEncode(values) & """" case "boolean" toJson="" & lcase(values) & "" case "date" toJson="""" & cstr(values) & """" case "empty" toJson="""Empty""" case "null" toJson="""Null""" case "nothing" toJson="""Nothing""" case "aien_json" toJson=getJson(values) case else if isnumeric(values) then toJson="" & cstr(values) & "" elseif isarray(values) then dim aStr:aStr="[" for i=0 to ubound(values) aStr=aStr & toJson(values(i)) if i<ubound(values) then aStr=aStr & "," next toJson="" & aStr & "]" else toJson="""" & cstr(values) & """" end if end select end function public function getJson(byval jsonObj) dim col:set col=jsonObj.Collection dim mJson if jsonObj.JsonType then mJson="{" else mJson="[" for each vars in col if jsonObj.JsonType then mJson=mJson & vars & ":" & toJson(col(vars)) else mJson=mJson & toJson(col(vars)) end if col.remove(vars) if col.count>0 then mJson=mJson & "," next if jsonObj.JsonType then mJson=mJson & "}" else mJson=mJson & "]" getJson=mJson end function Private Function jsEncode(str) Dim i, j, aL1, aL2, c, p aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09) aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74) For i = 1 To Len(str) p = True c = Mid(str, i, 1) For j = 0 To 7 If c = Chr(aL1(j)) Then jsEncode = jsEncode & "\" & Chr(aL2(j)) p = False Exit For End If Next If p Then Dim a a = AscW(c) If a > 31 And a < 127 Then jsEncode = jsEncode & c ElseIf a > -1 Or a < 65535 Then jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0") & Hex(a) End If End If Next End Function end class %>