Servlet中的代码
HashMap m = new HashMap();
m.put("totalCount",ucount);//总数据条数
m.put("topics",userList); //把取得的数据List,放到map中
Gson gson = new Gson();
String json = gson.toJson(m);//转换json对象为字符串json
response.getWriter().write(json); //返回json
js中的代码
var store = new Ext.data.JsonStore({
url: "griddata.jsp", //请求URL
root: "topics", //对应数据集的JSON KEY
idProperty: "threadid", //主键的KEY
//autoLoad : true, //自动加载
totalProperty: "totalCount",
//配置与返回的JSON数据的映射 name为store对应的 key mapping为返回JSON数据的KEY
fields : [
{name: 'ssex', mapping: 'sex'},//接收到json中的sex,并把它存到这个对象的ssex上
{name: 'susername', mapping: 'username'},
{name: 'sbirth', mapping: 'birth'},
{name: 'saddress', mapping: 'address'},
{name: 'sphone', mapping: 'phone'},
{name: 'semail', mapping: 'email'},
{name: 'sdes', mapping: 'des'}
]
});