jqGridTreeGrid demo json 解析

jqGridTreeGrid demo json 解析_第1张图片


package com.polaris.jqgrid.servlet;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JqTreeGridForJSONServlet extends HttpServlet
{
        private static final long serialVersionUID = 132383828833L;

        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                  JSONObject jsonObj = new JSONObject();
                // 根据jqGrid对JSON的数据格式要求给jsonObj赋值
                jsonObj.put("page", 4);                // 当前页
                jsonObj.put("total", 500);             // 总页数
                jsonObj.put("records",10000);          // 总记录数
                // 定义rows,存放数据
                JSONArray rows = new JSONArray();
                JSONArray cellArray = new JSONArray();
  
    // 放入10000条数据
                for(int i=1;i<10000;i++)
                {
                        // 存放一条记录的对象
                         List list=new ArrayList();
                        JSONObject cell = new JSONObject();
                        cell.put("id", i);
                        if(i==1)
                        {
                            list.add("amaxgs"+i);             //column1 menu
                            list.add("www.amaxgs.com");      //column2 url
                            list.add(0);                      //root
                            list.add(null);                   //left
                            list.add(false);                 //leaf
                            list.add(false);                 //expanded
      
                        }
                        else
                        {
                            list.add("joyopod");             //column1 menu
                            list.add("www.joyopod.com");     //column2 url
                            list.add(1);                     //root
                            list.add(1);                     //left
                            list.add(true);                  //leaf
                            list.add(false);                 //expanded
      
                        }     
                        cell.put("cell",list);
                        // 将该记录放入rows中
                        rows.add(cell);
                }
                // 将rows放入json对象中
                jsonObj.put("rows", rows);
                // 设置字符编码
                // 自控制台打印输出,以检验json对象生成是否正确
                System.out.println("要返回的json对象:\n" + jsonObj.toString());
                // 设置字符编码
                resp.setCharacterEncoding("UTF-8");
                // 返回json对象(通过PrintWriter输出)
                resp.getWriter().print(jsonObj.toString());
        }
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                doGet(req,resp);
        }
}
红色部分之前写错了,导致生成的json串格式不对,从第2个节点开始就无法显示了。

测试了10000个子节点,后台速度还可以。前台只显示了一页。

jsp 返回json 串正确格式如下:



你可能感兴趣的:(json,.net,jsp,servlet)