<%@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在显示中文时变乱码
转载请标明出处: 作者: 大灰狼
既然要用到json,那么就必须先生成json格式的文本,比较喜欢用java作为后台调试工具,所以写了一个java类来生成json:
程序代码
package com.zb.database.util;
import java.util.ArrayList;
public class Json {
public static void main(String[] args) {
//Json j=new Json();
//j.reSet();
//j.setSuccess(true);
//j.addItem("id", "1");
//j.addItem("name", "zb");
//j.addItemOk();
//j.addItem("id", "2");
//j.addItem("name", "jim");
//j.addItemOk();
//String dd=j.ToString();
//System.out.println(dd);
}
public String singleInfo="";
protected boolean _success=true;
protected String _
error="";
protected ArrayList arrData=new ArrayList();
protected ArrayList dataItem=new ArrayList();
public String getError() {
return _
error;
}
public void setError(String
error) {
if(!
error.equals(""))this._success=false;
this._
error =
error;
}
public boolean getSuccess() {
return _success;
}
public void setSuccess(boolean success) {
if(success) this._
error="";
this._success = success;
}
public Json()
{
}
public void reSet()
{
_success=true;
_
error="";
singleInfo="";
arrData.clear();
dataItem.clear();
}
public void addItem(String name,String _value)
{
dataItem.add(name);
dataItem.add(_value);
}
//一个数组添加完毕,一个新的数组开始
public void addItemOk()
{
arrData.add(dataItem);
dataItem=new ArrayList();
}
public String ToString()
{
StringBuilder sb=new StringBuilder();
sb.append("{");
sb.append("success:"+_success+",");
sb.append("
error:\""+_
error.replaceAll("\"","\\\"")+"\",");
sb.append("singleInfo:\""+singleInfo.replaceAll("\"","\\\"")+"\","+"
");
sb.append("data:["+"
");
int ad=arrData.size();
for(int i=0;i
{
ArrayList arr=(ArrayList)(arrData.get(i));
sb.append("{");
int t=arr.size();
for(int j=0;j {
if(j==t) break;
sb.append(arr.get(j).toString());
sb.append(":");
sb.append("\"");
sb.append(arr.get(j+1).toString());
sb.append("\"");
if(j }
sb.append("}");
if(i");
}
sb.append("
]");
sb.append("}");
return sb.toString();
}
}
<%@page import="com.zb.database.util.*,
com.zb.database.service.*,
com.zb.database.entity.*,
java.util.List"%>
<%
Json json=new Json();
UserService user = new UserService();
List list = user.getUserList();
Users users;
json.reSet();
json.setSuccess(true);
int j=list.size();
for (int i=0; i {
users = (Users) list.get(i);
json.addItem("userId",users.getId().toString());
json.addItem("name",users.getUserName());
json.addItem("pwd",users.getUserPwd());
json.addItem("age",users.getUserAge().toString());
json.addItemOk();
}
out.println(json.ToString());
%>
阅读(3031) | 评论(0) | 转发(0) |