怎样将查询到的List对象转换成JSON显示到DHTMLXGrid容器中?请高手指教,尽量用代码说明,谢谢。 5
2个回答
展开全部
dhtmlxGrid加载数据类型 —— json转换
List<Map<String, Object>> list = orderDAO.queryAllOrder(page); //取到数据集合
List<GridData> gridDataList = new ArrayList<GridData>();
Rows rows = new Rows(); //待转换为json数据的对象
for(Map<String, Object> map : list) {
gridDataList.add(this.BuildeJsonData(map));//组装数据,BuildeJsonData()方法返回的对象为grid中的一条数据GridData
}
rows.setRows(gridDataList);
JSONObject object = JSONObject.fromObject(rows); //转换为json对象
String str = object.toString();
//组装方法
private GridData BuildeJsonData(Map<String, Object> map){
GridData griddata = new GridData();
Set<String> set = map.keySet();
int i = 0;
String[] data = new String[7];
for(String key : set) {
Object obj = map.get(key);
String value = "";
if(null != obj) {
value = map.get(key).toString();
value = value == null ? "" : value;
}
//取记录id
if(i == 0) {
griddata.setId(value);
data[1] = value;
} else if(i == 1) {
data[2] = value;
} else if(i == 2) {
data[3] = value;
} else if(i == 3) {
data[4] = value;
} else if(i == 4) {
data[5] = value;
}
i++;
}
griddata.setData(data);
return griddata;
}
//数据对象
public class GridData {
private String id;
private String[] data;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String[] getData() {
return data;
}
public void setData(String[] data) {
this.data = data;
}
}
//数据集合
import java.util.List;
public class Rows {
private List rows;
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
//前台取到数据后,铺数据
var obj = eval('(' + data + ')');
mygrid.parse(obj, "json");
//数据结构如图
var js = {
"rows":[
{ "id":1000,
"data":[
"1000",
"Blood and Smoke",
{"value":"Stephen King", "type":"ed"},//change cell type
]
},
{ "id":1001,
"data":[
"1000",
"Blood and Smoke",
{"value":"Stephen King", "type":"ed"},//change cell type
]
}
]
};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询