我刚开始使用jpgrid。我在php json_encode中遇到了这个问题。
根据jqgrid网站的例子,json字符串可以通过php代码生成,如下所示:
$responce->page = "1";
$responce->total = 1;
$responce->records = "1";
$i=1; $amttot=0; $taxtot=0; $total=0;
while($i<=10) {
$amttot += $row[amount];
$taxtot += $row[tax];
$total += $row[total];
$responce->rows[$i]['id']=strval($i);
$responce->rows[$i]['cell']=array("123","2012-10-15","name","10000","tax","9999","note");
$i++;
}
$responce->rows = (array)$responce->rows;
$responce->userdata['amount'] = $amttot;
$responce->userdata['tax'] = $taxtot;
$responce->userdata['total'] = $total;
$responce->userdata['name'] = 'Totals:';
我遇到问题
$responce->rows[$i]['id']=strval($i);
$responce->rows[$i]['cell']=array("123","2012-10-15","name","10000","tax","9999","note");
当php json_encode生成json字符串时,rows数组被视为关联数组。因此结果json字符串看起来像:
{"page":"1","total":1,"records":"1",
"rows":{"1":{"id":"1","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"2":{"id":"2","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"3":{"id":"3","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"4":{"id":"4","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"5":{"id":"5","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"6":{"id":"6","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"7":{"id":"7","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"8":{"id":"8","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"9":{"id":"9","cell":["123","2012-10-15","name","10000","tax","9999","note"]},
"10":{"id":"10","cell":["123","2012-10-15","name","10000","tax","9999","note"]}},
"userdata":{"amount":0,"tax":0,"total":0,"name":"Totals:"}}
这个json字符串无法由jqgrid处理。实际上jqgrid不会引发任何javascript错误。它只是不呈现表内容。我通过firebugs查看了jqgrid示例网站上的json字符串。它看起来像这样:
{"page":"1","total":2,"records":"13",
"rows":[{"id":"13","cell":["13","2007-10-06","Client 3","1000.00","0.00","1000.00",null]},
{"id":"12","cell":["12","2007-10-06","Client 2","700.00","140.00","840.00",null]},
{"id":"11","cell":["11","2007-10-06","Client 1","600.00","120.00","720.00",null]},
{"id":"10","cell":["10","2007-10-06","Client 2","100.00","20.00","120.00",null]},
{"id":"9","cell":["9","2007-10-06","Client 1","200.00","40.00","240.00",null]},
{"id":"8","cell":["8","2007-10-06","Client 3","200.00","0.00","200.00",null]},
{"id":"7","cell":["7","2007-10-05","Client 2","120.00","12.00","134.00",null]},
{"id":"6","cell":["6","2007-10-05","Client 1","50.00","10.00","60.00",""]},
{"id":"5","cell":["5","2007-10-05","Client 3","100.00","0.00","100.00","no tax at all"]},
{"id":"4","cell":["4","2007-10-04","Client 3","150.00","0.00","150.00","no tax"]}],
"userdata":{"amount":3220,"tax":342,"total":3564,"name":"Totals:"}}
我无法弄清楚如何使用关联数组的php示例代码在json字符串中生成这样的数组obj。我使用的是PHP 5.4
我可以在jqgrid或php中更改以使其工作吗?我很惊讶没有人有这个问题。我可能会遗漏一些相当明显的东西。
感谢
casbby
答案 0 :(得分:0)
问题是由PHP中如何生成响应数组对象引起的。当我最初在循环中创建数组时,我从1而不是0开始索引。 响应 - &GT;行[I] 这导致php将响应数组视为关联而非顺序。因此php将名称值对象放在响应对象中(当我尝试创建一个非命名对象的数组时)
解决方案是确保数组索引是连续的和连续的