我在从无法访问的服务器上读取这种JSON时遇到麻烦,所以我无法操纵JSON:
{
"API_ICDFactory": {
"API_getDataSample": {
"key_0": {
"dateTransaction": "1379454296",
"dateBilling": "1387320296",
"units": "181",
"priceUnit": "25.12",
"amount": "4546.72",
"company": "Juan Vivas Taller",
"productRef": "CAR",
"productDesc": "Detergente especial antiestático"
},
"key_1": {
"dateTransaction": "1377421074",
"dateBilling": "1385373474",
"units": "137",
"priceUnit": "8.99",
"amount": "1231.63",
"company": "Autos Caceres 2000",
"productRef": "BRICAR",
"productDesc": "Cera hidrofugante para túneles de lavado"
},
"status": "success"
}
}
}
我并不完全知道如何从这个Json中获取元素。如果有人能帮助我做到这一点。
我正在尝试从jqGrid文档中操作这段代码,但我无法获得任何结果。
jQuery(document).ready(function(){
jQuery("#grid").jqGrid({
url: "url...",
datatype: "json",
mtype: "GET",
colNames: ['Fecha Trans','Fecha Pago', 'Cliente'],
colModel: [
{name:'dateTransaction',index:'dateTransaction', width:100,editable:true},
{name:'dateBilling',index:'dateBilling', width:100,editable:true},
{name:'company',index:'company', width:100,editable:true}
],
jsonReader: {
repeatitems:false
},
rowNum:10,
rowList:[10,20,30],
pager: jQuery('#pager'),
sortname: 'name',
viewrecords: true,
sortorder: 'asc',
caption:'Title',
editurl:'url...'
}).navGrid('#pager');
});
如果有人能告诉我这个JSON的正确语法,我可以做其余的事。
谢谢!
答案 0 :(得分:1)
您必须将从服务器返回的JSON数据转换为可由jqGrid读取的项目数组。另外,我建议你使用loadonce: true
选项,这样你就可以在jqGrid中使用本地分页,排序和过滤/灼热。
您可以执行转换输入数据,例如将root
jsonReader
部分定义为函数。或者,您可以使用beforeProcessing
回调来更改服务器响应。例如,您可以使用以下jsonReader
jsonReader: {
root: function (obj) {
var input = obj.API_ICDFactory.API_getDataSample, p, res = [], item;
for (p in input) {
item = input[p];
if (input.hasOwnProperty(p) && typeof item === "object") {
item.id = p;
res.push(item);
}
}
return res;
},
repeatitems: false
}
The demo证明了我的建议。它显示
我用过的完整代码可以在下面找到:
$(function () {
"use strict";
var intTemplate = { width: 100, formatter: "integer", sorttype: "integer", align: "right" };
$("#grid").jqGrid({
url: "victorgb6.json",
datatype: "json",
colNames: ["Fecha Trans", "Fecha Pago", "Cliente"],
colModel: [
{ name: "dateTransaction", template: intTemplate },
{ name: "dateBilling", template: intTemplate },
{ name: "company" }
],
cmTemplate: {width: 250, editable: true},
gridview: true,
height: "auto",
autoencode: true,
loadonce: true,
jsonReader: {
root: function (obj) {
var input = obj.API_ICDFactory.API_getDataSample, p, res = [], item;
for (p in input) {
item = input[p];
if (input.hasOwnProperty(p) && typeof item === "object") {
item.id = p;
res.push(item);
}
}
return res;
},
repeatitems: false
},
rowNum: 10,
rowList: [10, 20, 30],
pager: "#pager",
viewrecords: true,
caption: "Title",
editurl: "myEditUrl"
}).jqGrid("navGrid", "#pager");
});