首先要有这个jar包:
json-lib-2.2.3-jdk15.jar
项目代码中要导的包:
class="java" name="code">import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
1.java转json
List<Person> list=personService.pageByHql(hql, Integer.parseInt(pageNumber), Integer.parseInt(pageSize));
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
ServletActionContext.getResponse().getWriter().write((JSONArray.fromObject(list, jsonConfig)).toString());
2.json转java
private String jsonstr;//页面提交过来的json
JSONArray jsonArray = JSONArray.fromObject(jsonstr);
List<Person> list=(List<Person>)JSONArray.toCollection(jsonArray,Person.class);
3.页面中的变动数据改为json格式,即页面中的getChanges()方法
var rows = $('#dg').datagrid('getChanges');
var effectRow = new Object();
effectRow = JSON.stringify(rows);
$("#dg"):数据网格id
例:将json传到后台:
var rows = $('#dg').datagrid('getChanges');
var effectRow = new Object();
effectRow = JSON.stringify(rows);
$.ajax({
type:'POST',
url:'system/person_save.do',
datatype:'json',
data:(jsonstr:effectRow),
success:function(msg){
}
});