以下是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的具体内容是怎样的呢?它是数组还是对象还是字符串?麻烦高手们帮我写一句简单的示例,非常感谢!!!
json_note可以为数组,字典Scripting.Dictionary,asp的类实例之类的
帮助到你能点个采纳吗,谢谢~~
下面就是你要的结构
function createItem(id,age,sex,addtime)
set item=server.CreateObject("scripting.dictionary")
item.add "id",id
item.add "age",age
item.add "sex",sex
item.add "addtime",addtime
set createItem=item
end function
set dic=server.CreateObject("scripting.dictionary")
dic.Add "code",0
dic.Add "msg",""
dim arr(1)
set arr(0)=createItem(1,"23","男","2021/6/1 10:42:41")
set arr(1)=createItem(2,"24","女","2021/6/2 10:42:41")
set data=server.CreateObject("scripting.dictionary")
data.Add "item",arr
data.add "total",2
dic.Add "data",data
jsonStr=jsonObj.toJSON(Empty,dic,False)
response.Write(jsonStr)
json_note可以为数组,字典Scripting.Dictionary,asp的类实例之类的
帮助到你能点个采纳吗,谢谢~~
json_note=split("1,2,3,4",",")
jsonStr = jsonObj.toJSON(Empty,json_note,False)
response.Write(jsonStr)'["1","2","3","4"]
set dic=server.CreateObject("scripting.dictionary")
dic.Add "a",1
dic.Add "b",2
dic.Add "array",split("1,2,3,4",",")
jsonStr = jsonObj.toJSON(Empty,dic,False)
response.Write(jsonStr)'{"a": 1,"b": 2,"array": ["1","2","3","4"]}
JSON格式数据是这样的:
{ "people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
]}
JSON格式我是知道的,我只是问下json_note这个变量是什么东东?具体怎么写?
如果最终要显示这样的结果
{
"code":"0",
"msg":"",
"data":{
"item":[
{
"id":"17",
"age":"23",
"sex":"男",
"addtime":"2021/6/1 10:42:41"
}
],
"total":"1"
}
}
json_note应该怎么写呢?