从网上找了N个例子试用,每次都是在前台firebug中都已经可以看到传过来josn数据了,但是在jqgrid中就是没有数据。
今天把josn数据中的单引号,全部替换成双引号,数据终于显示出来了。
jqgrid居然不识别单引号!!!!!!
这个足足搞了三天,居然是因为这个。。。 郁闷!
<%@ page contentType="text/html;charset=utf-8"%> <link rel="stylesheet" type="text/css" href="public/lib/jquery/css/ui.jqgrid.css" /> <link rel="stylesheet" type="text/css" media="screen" href="public/lib/jquery/css/ui-lightness/jquery-ui-1.8.9.custom.css" /> <script src="public/lib/jquery/js/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="public/lib/jquery/js/i18n/grid.locale-cn.js" type="text/javascript"></script> <script src="public/lib/jquery/js/jquery.jqGrid.min.js" type="text/javascript"></script> <html> <head> <title>ALOES</title> <script type="text/javascript"> $(document).ready(function(){ jQuery("#jsonmap").jqGrid({ url : 'eventAction.do?method=getEvents', datatype : "json", colNames:['EventID','CUID', 'Date', 'Severity','EventType','Description'], colModel:[ {name:'eventID',index:'eventID', width:60}, {name:'cuid',index:'cuid', width:90}, {name:'eventTime',index:'eventTime', width:100}, {name:'severity',index:'severity', width:80, align:"right"}, {name:'eventType',index:'eventType', width:80, align:"right"}, {name:'description',index:'description', width:80,align:"right"} ], imgpath: '/public/images', rowNum:10, rowList:[10,20,30], pager: "pjmap", multiselect: false, sortname: 'eventID', viewrecords: true, sortorder: "desc", jsonReader: { root: "events", repeatitems : false }, caption: "jqGrid test", loadComplete:function(){ //根据内容的值 更换行的背景色 var n = $('#gridTable').getGridParam('records'); //取得现在表格中显示的数据条数 //alert(n); }, width:800, height: 220 }).navGrid('pjmap', {view:true,edit:true,add:false,del:false}, {closeOnEscape:true} ); }); </script> </head> <body> <table id="jsonmap" ></table> <div id="pjmap" ></div> </body> </html>
java后台代码如下:
随便写的测试代码,比较乱。。。
public void getEvents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { StringBuffer str = new StringBuffer(); //定义时间格式 DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { List eventList = eventService.getEvents(); str.append("{\"events\":["); if (eventList != null && eventList.size()> 0) { //for(Iterator iter = eventList.iterator();iter.hasNext();){ for(int i=0;i<10;i++){ Event event = (Event)eventList.get(i); str.append("{\"eventID\":"+"\""+event.getEventID()+"\","); str.append("\"cuid\":"+"\""+event.getCuid()+"\","); str.append("\"eventTime\":"+"\""+format1.format(event.getEventTime())+"\","); str.append("\"severity\":"+"\""+event.getSeverity()+"\","); str.append("\"eventType\":"+"\""+event.getEventType()+"\","); String dec = stringFilter(event.getDescription()); str.append("\"description\":"+"\""+dec+"\"}"); /* if(iter.hasNext()){ str.append(","); }*/ if(i<9){ str.append(","); } } } //str.substring(0,str.length()-1); str.append("],\"page\":1,\"rows\":10,\"total\":10}"); } catch(Exception e){ logger.error("获取告警事件发生异常!",e); } response.setContentType(" text/josn;charset=utf-8 "); PrintWriter out = response.getWriter(); out.println(str); out.flush(); }
接收到的josn数据:
{ "events":[ {"eventID":"1298878203692","cuid":"HP-3.125","eventTime":"2011-02-28 15:30:03","severity":"3","eventType":"Probe","description":" Connection refused"}, {"eventID":"1298878202225","cuid":"HP-3.125","eventTime":"2011-02-28 15:30:02","severity":"3","eventType":"Probe","description":"Connection refused"}, {"eventID":"1298878201201","cuid":"HP-3.125","eventTime":"2011-02-28 15:30:01","severity":"3","eventType":"Probe","description":" Connection refused"}, {"eventID":"1298877903783","cuid":"HP-3.125","eventTime":"2011-02-28 15:25:03","severity":"3","eventType":"Probe","description":"Connection refused"}, {"eventID":"1298877902365","cuid":"HP-3.125","eventTime":"2011-02-28 15:25:02","severity":"3","eventType":"Probe","description":" Connection refused"}, {"eventID":"1298877901326","cuid":"HP-3.125","eventTime":"2011-02-28 15:25:01","severity":"3","eventType":"Probe","description":"Connection refused"}, {"eventID":"1298867776540","cuid":"HP-3.125","eventTime":"2011-02-28 12:36:16","severity":"3","eventType":"Probe","description":"No route to host"}, {"eventID":"1298867776521","cuid":"Dell-31","eventTime":"2011-02-28 12:36:16","severity":"3","eventType":"Probe","description":"Connection timed out"}, {"eventID":"1298867776463","cuid":"HP-3.125","eventTime":"2011-02-28 12:36:16","severity":"3","eventType":"Probe","description":"Connection timed out"}, {"eventID":"1298867774532","cuid":"HPGroup","eventTime":"2011-02-28 12:36:14","severity":"3","eventType":"Probe","description":"Connection timed out"}], "page":1,"rows":10,"total":10 }
把这里边的双引号换成单引号数据就会无法显示!!!诡异。