⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 json.asp

📁 javasrcipt编写的Ajax的类库
💻 ASP
字号:
<%
'**************************************************************************************************************
'* GAB_LIBRARY Copyright (C) 2003 - This file is part of GAB_LIBRARY		
'* For license refer to the license.txt										
'**************************************************************************************************************

'**************************************************************************************************************

'' @CLASSTITLE:		JSON
'' @CREATOR:		Michal Gabrukiewicz (gabru at grafix.at), Michael Rebec
'' @CONTRIBUTORS:	- Cliff Pruitt (mail at cliffpruitt.com)
'' @CREATEDON:		2007-04-26 12:46
'' @CDESCRIPTION:	Comes up with functionality for JSON (http://json.org) to use within ASP.
'' 					Correct escaping of characters, generating JSON Grammer out of ASP datatypes and structures
'' @REQUIRES:		-
'' @VERSION:		1.1

'**************************************************************************************************************
class JSON

	'private members
	private output, recursiveGenerationCall
	
	'public members
	public toResponse		''[bool] should generated results be directly written to the response? default = false
	
	'**********************************************************************************************************
	'* constructor 
	'**********************************************************************************************************
	public sub class_initialize()
		newGeneration()
		toResponse = false
	end sub
	
	'******************************************************************************************
	'' @SDESCRIPTION:	STATIC! takes a given string and makes it JSON valid
	'' @DESCRIPTION:	all characters which needs to be escaped are beeing replaced by their
	''					unicode representation according to the 
	''					RFC4627#2.5 - http://www.ietf.org/rfc/rfc4627.txt?number=4627
	'' @PARAM:			val [string]: value which should be escaped
	'' @RETURN:			[string] JSON valid string
	'******************************************************************************************
	public function escape(val)
		cDoubleQuote = &h22
		cRevSolidus = &h5C
		cSolidus = &h2F
		
		for i = 1 to (len(val))
			currentDigit = mid(val, i, 1)
			if asc(currentDigit) > &h00 and asc(currentDigit) < &h1F then
				currentDigit = escapequence(currentDigit)
			elseif asc(currentDigit) >= &hC280 and asc(currentDigit) <= &hC2BF then
				currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
			elseif asc(currentDigit) >= &hC380 and asc(currentDigit) <= &hC3BF then
				currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC2C0), 2, 0), 2)
			else
				select case asc(currentDigit)
					case cDoubleQuote: currentDigit = escapequence(currentDigit)
					case cRevSolidus: currentDigit = escapequence(currentDigit)
					case cSolidus: currentDigit = escapequence(currentDigit)
				end select
			end if
			escape = escape & currentDigit
		next
	end function
	
	'******************************************************************************************************************
	'' @SDESCRIPTION:	generates a representation of a name value pair in JSON grammer
	'' @DESCRIPTION:	the generation is done fully recursive so the value can be a complex datatype as well. e.g.
	''					toJSON("n", array(array(), 2, true), false) or toJSON("n", array(RS, dict, false), false), etc.
	'' @PARAM:			name [string]: name of the value (accessible with javascript afterwards). leave empty to get just the value
	'' @PARAM:			val [variant], [int], [float], [array], [object], [dictionary], [recordset]: value which needs
	''					to be generated. Conversation of the data types (ASP datatype -> Javascript datatype):
	''					NOTHING, NULL -> null, ARRAY -> array, BOOL -> bool, OBJECT -> name of the type, 
	''					DICTIONARY -> valuepairs. each key is accessible as property afterwards
	''					RECORDSET -> array where each row of the recordset represents a field in the array. 
	''					fields have properties named after the column names of the recordset (LOWERCASED!)
	''					e.g. generate(RS) can be used afterwards r[0].ID
	''					INT, FLOAT -> number
	''					OBJECT with reflect() method -> returned as object which can be used within JavaScript
	'' @PARAM:			nested [bool]: is the value pair already nested within another? if yes then the {} are left out.
	'' @RETURN:			[string] returns a JSON representation of the given name value pair
	''					(if toResponse is on then the return is written directly to the response and nothing is returned)
	'******************************************************************************************************************
	public function toJSON(name, val, nested)
		if not nested and not isEmpty(name) then write("{")
		if not isEmpty(name) then write("""" & escape(name) & """: ")
		generateValue(val)
		if not nested and not isEmpty(name) then write("}")
		toJSON = output
		
		if not recursiveGenerationCall then newGeneration()
	end function
	
	'******************************************************************************************************************
	'* generate 
	'******************************************************************************************************************
	private function generateValue(val)
		if isNull(val) then
			write("null")
		elseif isArray(val) then
			write("[")
			for i = 0 to uBound(val)
				if i > 0 then write(",")
				generateValue(val(i))
			next
			write("]")
		elseif isObject(val) then
			if val is nothing then
				write("null")
			elseif typename(val) = "Dictionary" then
				recursiveGenerationCall = true
				write("{")
				keys = val.keys
				for i = 0 to uBound(keys)
					if i > 0 then write(",")
					toJSON keys(i), val(keys(i)), true
				next
				write("}")
				recursiveGenerationCall = false
			elseif typename(val) = "Recordset" then
				write("[")
				while not val.eof
					recursiveGenerationCall = true
					write("{")
					for i = 0 to val.fields.count - 1
						if i > 0 then write(",")
						toJSON lCase(val.fields(i).name), val.fields(i).value, true
					next
					write("}")
					val.movenext()
					if not val.eof then write(",")
					recursiveGenerationCall = false
				wend
				write("]")
			else
				on error resume next
				set props = val.reflect()
				if err = 0 then
					on error goto 0
					toJSON empty, props, true
				else
					on error goto 0
					write("""" & escape(typename(val)) & """")
				end if
			end if
		else
			'bool
			if varType(val) = 11 then
				if val then write("true") else write("false")
			'integer
			elseif varType(val) = 2 or varType(val) = 3 then
				write(cLng(val))
			'floats
			elseif varType(val) = 4 or varType(val) = 5 then
				write(replace(cDbl(val), ",", "."))
			else
				write("""" & escape(val & "") & """")
			end if
		end if
		generateValue = output
	end function
	
	'******************************************************************************************************************
	'* newGeneration 
	'******************************************************************************************************************
	private sub newGeneration()
		output = empty
		recursiveGenerationCall = false
	end sub
	
	'******************************************************************************************
	'* JsonEscapeSquence 
	'******************************************************************************************
	private function escapequence(digit)
		escapequence = "\u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
	end function
	
	'******************************************************************************************
	'* padLeft 
	'******************************************************************************************
	private function padLeft(value, totalLength, paddingChar)
		padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
	end function
	
	'******************************************************************************************
	'* clone 
	'******************************************************************************************
	public function clone(byVal str, n)
		for i = 1 to n : clone = clone & str : next
	end function
	
	'******************************************************************************************
	'* write 
	'******************************************************************************************
	private sub write(val)
		if toResponse then
			response.write(val)
		else
			output = output & val
		end if
	end sub

end class
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -