【问题标题】:jgGrid not displaying json datajqGrid不显示json数据
【发布时间】:2010-07-02 08:58:11
【问题描述】:

这让我抓狂,所以任何帮助都会很棒:

我的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']}]}

但网格中没有显示任何数据。

【问题讨论】:

  • 最好使用一些标准的序列化程序,而不是手动 JSON 序列化:例如 ScriptSerializer.SerializeDataContractJsonSerializer.WriteObject。您也可以使用来自json.codeplex.com 的 Json.net。

标签: json jqgrid


【解决方案1】:

你也可以创建类似的东西:

public class JqGridJsonData
{
   public int Total {get;set;}
   public int Page {get;set;}
   etc
}

并使用 Json.NET 将其序列化为 json http://james.newtonking.com/pages/json-net.aspx

【讨论】:

    【解决方案2】:

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

    所以cs的输出json应该是:

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

    【讨论】:

    • 正确的是 JSON 仅支持双引号(请参阅 json.org)您可以在 jsonlint.com 上验证您的 JSON 数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多