2010-07-02 111 views
1

这是推动我疯狂,所以任何帮助将是真棒:jgGrid不显示JSON数据

我的HTML是如下:

<asp:Content ID="Content3" ContentPlaceHolderID="Content" runat="server" class="MainLoginScreen"> 
     <table id="StudyTable"></table> 
     <div id="StudiesPager"></div> 
</asp:Content> 

我的JavaScript如下:

<script language="javascript" type="text/javascript"> 
     $(document).ready(function() { 
      $("#StudyTable").jqGrid({ 
       url: '/StudyManager/GetStudyTable.aspx', 
       datatype: "json", 
       mtype:"GET", 
       colNames: ['Name', 'URL', 'Creation Date', 'Description', 'Status'], 
       colModel:[ 
        { name: 'Name', width: 200}, 
        { name: 'URL', width: 100 }, 
        { name: 'Creation_Date', width: 300}, 
        { name: 'Description', width: 200 }, 
        { name: 'Status', width: 200} 
       ], 
      rowNum:10, 
      rowList:[10,20,30], 
      viewrecords: true, 
      sortname: 'Name', 
      sortorder: "asc", 
      pager: $('#StudiesPager'), 
      imgpath: '/Content/Images', 
      jsonReader: { 
       root: "rows", 
       page: "page", 
       total: "total", 
       records: "records", 
       repeatitems: true, 
       cell: "cell", 
       id: "id" 
      }, 
      width: 800, 
      height: 400 
      }); 
     }); 
    </script> 

和我的cs码是:

protected void Page_Load(object sender, EventArgs e) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.Append("{'total':1,"); 
      sb.Append("'page':'1',"); 
      sb.Append("'records':'1',"); 
      sb.Append("'rows': ["); 
      sb.Append("{ 'id':'123', 'cell':['abc','abc','abc','abc','abc']}"); 
      sb.Append("]}"); 

      Response.Clear(); 
      Response.StatusCode = 200; 
      Response.Write(sb.ToString()); 
      Response.End(); 
     } 

表和寻呼机显示完美,但没有数据呈现到表中。 从我的CS返回的JSON似乎是正确的格式:

{'total':1,'page':'1','records':'1','rows': [{ 'id':'123', 'cell'['abc','abc','abc','abc','abc']}]} 

但没有数据显示在网格中。

+1

到JSON例如,应该更好地使用一些标准序列化程序,而不是手动JSON序列化:'ScriptSerializer.Serialize'或'DataContractJsonSerializer.WriteObject'。你也可以从http://json.codeplex.com/使用Json.net。 – Oleg 2010-07-02 10:57:52

回答

1

所有可能导致问题的事情 - 都是单引号。 JSON似乎不允许“单词”,而是需要“单词”。

所以从CS输出JSON应该是:

{"total":"1","page":"1","records":"1","rows": [{ "id":"123", "cell"["abc","abc","abc","abc","abc"]}]} 
+1

这是正确的,JSON支持双引号(请参阅http://www.json.org/)您可以在http://www.jsonlint.com/上验证您的JSON数据。 – Oleg 2010-07-02 10:45:10