以下是ASP转JSON的类文件代码
<%
class JSON
private output, innerCall
public toResponse
public sub class_initialize()
newGeneration()
toResponse = false
end sub
PUBLIC FUNCTION escape(val)
dim cDoubleQuote, cRevSolidus, cSolidus
cDoubleQuote = &h22
cRevSolidus = &h5C
cSolidus = &h2F
dim i, currentDigit
for i = 1 to (len(val))
currentDigit = mid(val, i, 1)
if ascw(currentDigit) > &h00 and ascw(currentDigit) < &h1F then
currentDigit = escapequence(currentDigit)
elseif ascw(currentDigit) >= &hC280 and ascw(currentDigit) <= &hC2BF then
currentDigit = "\u00" + right(padLeft(hex(asc(currentDigit) - &hC200), 2, 0), 2)
elseif ascw(currentDigit) >= &hC380 and ascw(currentDigit) <= &hC3BF then
currentDigit = "\u00" + right(padLeft(hex(ascw(currentDigit) - &hC2C0), 2, 0), 2)
else
select case ascw(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
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 innerCall = 0 then newGeneration()
END FUNCTION
PRIVATE FUNCTION generateValue(val)
if isNull(val) then
write("null")
elseif isArray(val) then
generateArray(val)
elseif isObject(val) then
if val is nothing then
write("null")
elseif typename(val) = "Dictionary" then
generateDictionary(val)
elseif typename(val) = "Recordset" then
generateRecordset(val)
else
generateObject(val)
end if
else
varTyp = varType(val)
if varTyp = 11 then
if val then write("true") else write("false")
elseif varTyp = 2 or varTyp = 3 or varTyp = 17 or varTyp = 19 then
write(cLng(val))
elseif varTyp = 4 or varTyp = 5 or varTyp = 6 or varTyp = 14 then
write(replace(cDbl(val), ",", "."))
else
write("""" & escape(val & "") & """")
end if
end if
generateValue = output
END FUNCTION
PRIVATE SUB generateArray(val)
dim item, i
write("[")
i = 0
for each item in val
if i > 0 then write(",")
generateValue(item)
i = i + 1
next
write("]")
END SUB
PRIVATE SUB generateDictionary(val)
dim keys, i
innerCall = innerCall + 1
write("{")
keys = val.keys
for i = 0 to uBound(keys)
if i > 0 then write(",")
toJSON keys(i), val(keys(i)), true
next
write("}")
innerCall = innerCall - 1
END SUB
PRIVATE SUB generateRecordset(val)
dim i
write("[")
while not val.eof
innerCall = innerCall + 1
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(",")
innerCall = innerCall - 1
wend
write("]")
END SUB
PRIVATE SUB generateObject(val)
dim props
on error resume next
set props = val.reflect()
if err = 0 then
on error goto 0
innerCall = innerCall + 1
toJSON empty, props, true
innerCall = innerCall - 1
else
on error goto 0
write("""" & escape(typename(val)) & """")
end if
END SUB
PRIVATE SUB newGeneration()
output = empty
innerCall = 0
end sub
PRIVATE FUNCTION escapequence(digit)
escapequence = "\u00" + right(padLeft(hex(asc(digit)), 2, 0), 2)
END FUNCTION
PRIVATE FUNCTION padLeft(value, totalLength, paddingChar)
padLeft = right(clone(paddingChar, totalLength) & value, totalLength)
END FUNCTION
PUBLIC FUNCTION clone(byVal str, n)
dim i
for i = 1 to n : clone = clone & str : next
END FUNCTION
PRIVATE SUB write(val)
if toResponse then
response.write(val)
else
output = output & val
end if
END SUB
end class
%>
调用的时候是这样的
Set jsonObj=New json
jsonObj.toResponse=False
代码省略。。。
jsonStr = jsonObj.toJSON(Empty,json_note,False)
response.Write(jsonStr)
请问一下,代码中json_note的具体内容是怎样的呢?它是数组还是对象还是字符串?麻烦高手们帮我写一句简单的示例,非常感谢!!!