1.
JSON对象: var memberJson = { "loginName":"", "currentService":"", "realName":"", "userCompType":"-1", "serviceStatus":"-1", "isActive":"-1", "email":"", "regionId":0, "createdStart":"", "createdEnd":"", "serviceStartDateStart":"", "serviceStartDateEnd":"", "serviceEndDateStart":"", "serviceEndDateEnd":"", "lastLoginStart":"", "lastLoginEnd":"" };
为JSON赋值: memberJson.loginName = $("#loginName").val(); memberJson.currentService = $("#currentService").val(); memberJson.realName = $("#realName").val(); memberJson.userCompType = $("#userCompType").val(); memberJson.serviceStatus = $("#serviceStatus").val(); memberJson.isActive = $("#isActive").val(); memberJson.email = $("#email").val(); memberJson.regionId = $("#regionId").val(); memberJson.createdStart = $("#createdStart").val(); memberJson.createdEnd = $("#createdEnd").val(); memberJson.serviceStartDateStart = $("#serviceStartDateStart").val(); memberJson.serviceStartDateEnd = $("#serviceStartDateEnd").val(); memberJson.serviceEndDateStart = $("#serviceEndDateStart").val(); memberJson.serviceEndDateEnd = $("#serviceEndDateEnd").val(); memberJson.lastLoginStart = $("#lastLoginStart").val(); memberJson.lastLoginEnd = $("#lastLoginEnd").val();
将JSON对象转化为字符串,用于传输后台. var memberify = JSON.stringify(memberJson); //条件
后台里再将JSON对象转化成javaBean:
import net.sf.json.JSONObject;
JSONObject jo = JSONObject.fromObject(memberJon);
UsersVO findParamet = (UsersVO)JSONObject.toBean(jo,UsersVO.class);
findParamet 就是封装了N个查询条件的VO对象了.
2.
在JAVA中传输到页面的对象若为List,可以用JSONArray:
JSONArray jsonArray = JSONArray.fromObject(new List<Region>);
页面接到传过来的值要转化下: var regions = JSON.parse(regionList); var len = regions.length; var optCity="<option value='-1' selected='selected'>请选择市</option>"; for(var i=0; i<len;i++){ optCity = optCity+"<option value='"+regions[i].regionId+"'>"+regions[i].regionName+"</option>"; } $("select[name='cityID']").html(optCity); $("select[name='cityID']").attr("value",'-1');