jqGrid没有显示来自java的Json数据
jqGrid not displaying the Json data from java
这是我的java服务
@RequestMapping(value="/refreshInterviewServiceList1", method = RequestMethod.GET)
public @ResponseBody JSONObject refreshInterviewServiceList1() throws JSONException{
List<ReportInterview> reportInterview = reportInterviewServiceImpl.findByReportId(reportId);
JSONObject output = new JSONObject();
JSONArray toplevel = new JSONArray();
output.put("page", 1);
output.put("records", reportInterview.size());
output.put("total",1);
for(int i = 0;i<reportInterview.size();i++){
JSONObject data = new JSONObject();
JSONArray data1 = new JSONArray();
data.put("id", i+1);
data1.put(reportInterview.get(i).getName());
data1.put(reportInterview.get(i).getOccupation());
data.put("cell", data1);
toplevel.put(data);
}
output.put("rows", toplevel);
System.out.println(output);
return output;
}
这是我的javascript
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#projectTable").jqGrid({
url: 'refreshInterviewServiceList1',
datatype: "json",
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
cell: "cell",
id: "id"
},
colNames:['Id','Name','Occupation'],
colModel:[
{name:'id',index:'id', width:10},
{name:'name',index:'name', width:10},
{name:'occupation',index:'occupation', width:10}
],
rowNum:10,
rowList:[10,20,30],
height:460,
width:700,
pager: "#pagingDiv",
viewrecords: true,
caption: "Projects"
});
});
json对象的输出
{
"total": 1,
"page": 1,
"records": 2,
"rows": [
{
"id": 1,
"cell": [
"232",
"12"
]
},
{
"id": 2,
"cell": [
"45",
"454"
]
}
]
}
我现在的问题是jqgrid无法在jqgrid中显示json数据。。。仅显示空白列表。。。请帮忙。。。谢谢
这是我前段时间为个人项目编写的一个实用程序。它生成jqGrid可以渲染的数据。
import java.util.List;
public class JQGridContainer {
private Integer page;
private Integer total;
private Integer records;
private List<JQGridRow> rows;
//getters and setters are omitted for brevity
}
import java.util.List;
public class JQGridRow {
private Integer id;
private List<String> cell;
//getters and setters are omitted for brevity
}
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.Logger;
public class JQGridFormatterUtil {
private static final Logger logger = Logger
.getLogger(JQGridFormatterUtil.class);
public static String getJSON(int currentPageNo,
int totalRecords, Set objectsToBeAdded,
List orderedPropertyNames) {
Integer pages = 0;
if(totalRecords % 50 >0){
pages = (totalRecords/50)+1;
}
else{
pages = (totalRecords/50);
}
JQGridContainer container = new JQGridContainer();
container.setPage(currentPageNo);
container.setTotal(pages);
container.setRecords(totalRecords);
List rows = new ArrayList();
if (!objectsToBeAdded.isEmpty()) {
for (Object obj : objectsToBeAdded) {
JQGridRow row = new JQGridRow();
row.setId(new Integer(getPropertvalue(obj, "id")));
List cells = new ArrayList();
for (String propertyName : orderedPropertyNames) {
cells.add(getPropertvalue(obj, propertyName));
}
row.setCell(cells);
rows.add(row);
}
}
container.setRows(rows);
return JSONUtil.convertToJSON(container);
}
private static String getPropertvalue(Object bean, String propName) {
String val = null;
try {
val = ObjectUtils.toString(PropertyUtils.getProperty(bean, propName));
} catch (IllegalAccessException e) {
logger.error(e);
} catch (InvocationTargetException e) {
logger.error(e);
} catch (NoSuchMethodException e) {
logger.error(e);
}
return val;
}
import org.apache.log4j.Logger;
import com.google.gson.Gson;
public class JSONUtil {
private static final Logger logger = Logger.getLogger(JSONUtil.class);
public static String convertToJSON(Object obj){
String json = null;
Gson gson = new Gson();
json=(gson.toJson(obj));
logger.debug(json);
return json;
}
}
}
这不是你的问题的解决方案,但它可能会帮助你避免未来的错误:
请从你的代码中删除comma
,因为它不是必需的,当我犯同样的错误时,它在IE(8)中给出了错误
{name:'occupation',index:'occupation', width:10}, //Remove the comma at last position
并且来自以下行:
id: "id", //Remove the comma at last position
相关文章:
- 如何使用json将对象列表从java转换为javascript
- DOM事件通过JSON转换为java
- JSON分析错误:Java中AJAX、Javascript和Servlet的意外EOF
- 错误405:向Java控制器(Ajax)发送JSON时找不到POST方法
- 在JSON.parse Javascript中字符串化Java对象抛出错误
- 将Java对象转换为JSON
- 从 JSON 获取键值对而不使用 .(DOT) 在 Java 脚本或 jquery 中
- 解析从js发送到Java的json字符串时出错
- 将JSON数组从PHP传递到Java脚本
- 有些人有任何关于如何使用Modified Java Script Value处理返回Json(url)的示例
- 如何用javascript访问json对象并用java将其转换为数组
- 如何在java框架(JSF)中基于URL的json响应生成动态表单
- 解析格式错误的 json 字符串,该字符串周围没有双引号(Java 脚本)
- JQuery 自动完成与 java 使用多个 JSON 值
- 从 Java 服务器加载 JSON ajax 后无法获取 h1 标记的内部 HTML
- 使用 json ajax javascript jquery 和 java 的自动完成搜索栏(无 PHP)
- 使用 ajax,jquery ,java 保存大型 JSON 字符串
- 无法解析javascript中的json字符串,这是java方法的响应
- 在 java 脚本中从 JSON 加载内容之前返回值
- Android java json parse empty