`

JSON、JAVA互转与将页面中改变的数据转为json格式

    博客分类:
  • json
阅读更多
首先要有这个jar包:
json-lib-2.2.3-jdk15.jar

项目代码中要导的包:
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){

				}
			});


4.存在主外键关联时的取值:
<th data-options="field:'organization',width:'65px',align:'center',editor:'text',
	formatter:function(value,rec,index){
		var name='';
		if(value!=null){
			name= value.name;
		}
		return name;
	}">机构</th>
1
1
分享到:
评论
1 楼 longhua2003 2015-02-07  
非常实用,朋友

相关推荐

Global site tag (gtag.js) - Google Analytics