如何在jqGrid中使用JSON字符串或JSON对象?

lea*_*tes 8 jquery json jqgrid

当我的JSON数据在静态文件中时,我的jqGrid工作,但是如果我将数据复制到var然后尝试将var加载到jqGrid的url中,它就不会显示.

你能把字符串传递给jqGrid吗?

这样可行:

function GetJSON() {
    var jsonFile = "EntityWithChildren.json";
    return jsonFile;//returning a file works fine.
}

$("#jsonmap").jqGrid({
    url: GetJSON(),
    datatype: 'json',
Run Code Online (Sandbox Code Playgroud)

这不是:

function GetJSON() {
    var json = '{"page":"1","total":"10",   "records":"10", "Entities": [       {"Fields":["Entity1", "field1", "11"]},     {"Fields":["", "field2", "22"]},        {"Fields":["Entity2", "field3", "33"]},     {"Fields":["ChildEntity1", "cfield1", "111"]}   ]}';
    return json; //doesnt work

}

$("#jsonmap").jqGrid({
    url: GetJSON(),
    datatype: 'json',
    //datatype: 'jsonstring',//this doesnt work either
Run Code Online (Sandbox Code Playgroud)

lea*_*tes 16

得到它了.需要使用datastr而不是url

datatype: 'jsonstring',
datastr: GetJSON(),
Run Code Online (Sandbox Code Playgroud)