jsp从数据库获得数据怎么穿花为JSON,然后传给store,请给出实例代码,谢谢!

解决方案 »

  1.   


    toJson 这个方法在那个类里面?(包org.json),谢谢!
      

  2.   

    Object.toJson(数据值)建议不要用,当对象里存在列表等集合时,会发生错误,还是一个属性一个属性的转吧
      

  3.   

    JSONObject
    你可以查看下json.jar。以及搜下对应的api
      

  4.   

    很多这种的jar包吧,导入就可以用
    JSONObject.fromObject(Object);
      

  5.   

    JSONObject
       哈哈 全写了烦 你上网看看 很多的例子
      

  6.   


    <%
        //Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        Class.forName("com.mysql.jdbc.Driver");   
        //tring url ="jdbc:mysql://localhost:3306/book?username=root&password=123456"; 
        //testDB为你的数据库名  
        //Connection conn= DriverManager.getConnection(url);
        String url = "jdbc:mysql://127.0.0.1/testorder?useUnicode=true&characterEncoding=utf-8";   
        String user = "root";   
        String psw = "123456";   
        Connection conn = DriverManager.getConnection(url,user,psw); 
         
        Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        String sql="select * from ordertable"; 
        ResultSet rs=stmt.executeQuery(sql);
        int i=1; 
        String result=null;
        String rowcount=null;
        JSONObject jsonObject = new JSONObject();   
        while(rs.next()) {
        
           //Map<String,Object> tree = new HashMap<String,Object>();
           //tree.put("id",i);
           //tree.put("dorder", rs.getString(1));
           //tree.put("xdate", rs.getString(2));
           
      
       
      
         
       
       jsonObject.put("id", (Object)rs.getString(1));     
       jsonObject.put("dorder", (Object)rs.getString(2));    
       jsonObject.put("xdate", (Object)rs.getString(3));    
     
         
         
       
       
        if (i==1)
       {
            
             result = jsonObject.toString();
        }
        else
        {
             result += "," + jsonObject.toString();
        }
       
       i++;
       
       
       }
       
       //System.out.println(jsonObject.toString());
       rs.close(); 
       stmt.close(); 
       conn.close(); 
       rowcount=String.valueOf(i-1);
       response.getWriter().write("{total:"+rowcount+",stocks:[" + result + "]}");
       %> 
    GetorderData.jspvar typeStore = new Ext.data.Store({//配置分组数据集
    //autoLoad :true,
    reader: new Ext.data.XmlReader({
    totalRecords: "total",
    record: "stocks",
    id: "id"
    },
    Ext.data.Record.create([
    {name: 'id'},
    {name: 'dorder'},
    {name: 'xdate'}
    ])
    ),
    proxy : new Ext.data.HttpProxy({
    url : 'pagesExt/GetOrderData.jsp'
    })
    })
    var cb = new Ext.grid.CheckboxSelectionModel()
    var bookTypeGrid = new Ext.grid.GridPanel({
    applyTo : 'grid-div',
    tbar : toolbar,
    frame:true,
    store: typeStore,
    viewConfig : {
    autoFill : true
    },
    sm : cb,
    columns: [//配置表格列
    new Ext.grid.RowNumberer({
    header : '行号',
    width : 40
    }),//表格行号组件
    cb,
    {header: "编号", width: 80, dataIndex: 'id', sortable: true,hidden:true},
    {header: "单号", width: 180, dataIndex: 'dorder', sortable: true},
    {header: "下单日期", width: 280, dataIndex: 'xdate', sortable: true}
    ],
     bbar: new Ext.PagingToolbar({
            pageSize: 10,  
            store: typeStore,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
            emptyMsg: "没有记录"
         }),
        
         renderTo:'grid'
        
    });
    typeStore.load({params:{start:0, limit:10}});
    怎么grid里面显示不了数据啊?