ASP生成JSON数据

原文地址为:ASP生成JSON数据

< %@LANGUAGE = " VBSCRIPT "  CODEPAGE = " 65001 " % > 
 < ! -- #include file = " json.asp " --> 
 < ! -- #include file = " inc/Conn.asp "   --> 
 < %
response.ContentType = " text/json " 
 dim  j

 ' 多重嵌套的JSON,要使用Dictionary才能实现 
 set  j = new  json
j.toResponse = false 
 set  r = server.createobject( " scripting.dictionary " )
 set  b = server.createobject( " scripting.dictionary " )
 set  c = server.createobject( " scripting.dictionary " )
c.add  " x " , 5 
c.add  " y " , 6 
c.add  " z " , 11 
b.add  " event " , " Mouse Click " 
b.add  " data " ,c
r.add  " success " , true 
r.add  " result " ,b
a = j.toJSON( empty ,r, false )
response.write a

 ' 记录集转为Json 
 set  i = new  json
i.toresponse = false 
 set  rs = server.CreateObject( " adodb.recordset " )
sqlstr = " select top 5 * from producttype " 
rs.open sqlstr,conn, 0 , 1 
v = i.toJson( " result " ,rs, false )
rs.close
response.write v
% >

面要注意的是:

1,嵌套的JSON是要用DICTIONARY来生成的,其他的方法,我还没有找到

2,最终的结果在输出时,可以用toJson(empty,r,false)这种形式,这样,生成的JSON,就不会类似{var:{var1:'',var2:''}}这种形式,而是直接的:{var1:'',var2:''},这种格式在我们用来处理表单,返回数据时,一定要注意

3,生成JSON的ASP页面,输出格式,解析JSON的JS文件,HTML页面,必须全是UTF-8格式编码,一个环节编码不正确,就会导致接收到的JSON在显示中文时变乱码


转载请注明本文地址:ASP生成JSON数据

原文地址:https://www.cnblogs.com/apolloren/p/10424984.html