java中的JSON使用

1、json的官网:http://www.json.org/

2、使用java的jar包:http://json-lib.sourceforge.net/

这个包依赖的东西比较多,官网介绍如下:

 

Json-lib requires (at least) the following dependencies in your classpath:

  • jakarta commons-lang 2.5
  • jakarta commons-beanutils 1.8.0
  • jakarta commons-collections 3.2.1
  • jakarta commons-logging 1.1.1
  • ezmorph 1.0.6

3、从业务逻辑层获取数据形成json格式的字符串

 

//stageList就是我们从业务逻辑层中获取的数据
private String parseStageToStr(List<Stage> stageList) {
		
		StringBuilder sb = new StringBuilder(200);
		if(CollectionUtils.isEmpty(stageList)) {
			return "";
		}
		int size = stageList.size();
		
		for(int i = 0; i < size; i++) {
			Stage s = stageList.get(i);
			sb.append(s.getStageId() + ":{");
			StringBuilder tmp = new StringBuilder(200);
			tmp.append("projectId:\""+ (s.getProjectId())+"\",");
			tmp.append("subProjectId:\""+(s.getSubProjectId())+"\",");
			tmp.append("stageNameId:\""+(s.getStageNameId())+"\",");
			tmp.append("psName:\"" + (s.getPsName()==null?"":s.getPsName())+"\",");
			tmp.append("planFromDate:\"" + (DateUtil.dateToStr(s.getPlanFromDate()))+"\",");
			tmp.append("planToDate:\"" + (DateUtil.dateToStr(s.getPlanToDate()))+"\",");
			tmp.append("stageLogId:\"" + (s.getStageLogId())+"\"");
			//tmp.append("departmentIds:\""+(s.getDepartmentIds()==null?"":s.getDepartmentIds())+"\"");
			sb.append(tmp);
			sb.append("}");
			if(i < size -1) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

 

 

4、在jsp页面中,解析json字符串

a)上面封装的json数据格式:{"265":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"2",planFromDate:"",planToDate:"",stageLogId:"100"},"270":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"",planFromDate:"",planToDate:"",stageLogId:"100"},"268":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"",planFromDate:"",planToDate:"",stageLogId:"100"}}

   b)js中使用

var json = {<s:property value="#request.jsonStr" escape="false"/>};
	//alert('<s:property value="#request.jsonStr" escape="false"/>');
	function initTbody(){
		var str = '';
		for(var k in json){
			var v = json[k];
			
			var tr = '<tbody stageLogId="' + v.stageLogId
				+ '" projectId="' + v.projectId
				+ '" subProjectId="' + v.subProjectId
				+ '">';
			
			var stages = v.stages, idx = 0;
			for(var m in stages){
				tr += '<tr planFromDate="' + stages[m].planFromDate + '" planToDate="' + stages[m].planToDate + '" stageNameId="' + stages[m].stageNameId + '" psName="' + stages[m].psName + '" stageId="' + m + '" class="datarow" ondblclick="editStage(this)">';
				
	}
 

5、jsp页面通过**.action?jsonParam="这里是json格式的数据"传入到逻辑层处理

 

/**
	 * 解析json数据
	 * @param str//格式:{"265":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"2",planFromDate:"",planToDate:"",stageLogId:"100"},"270":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"",planFromDate:"",planToDate:"",stageLogId:"100"},"268":{projectId:"0",subProjectId:"126",stageNameId:"1",psName:"",planFromDate:"",planToDate:"",stageLogId:"100"}}
	 * @return
	 * 2012-5-2 下午10:14:24
	 */
	private List<Stage> parseStageJson(String str) {
		
		List<Stage> stageList = new ArrayList<Stage>();
		if(StringUtils.isEmpty(str)) return stageList;
		JSONObject array = JSONObject.fromObject(str);
		Iterator<String> keys = array.keys();
		while(keys.hasNext()) {
			String key = keys.next();
			System.out.println(key);
			JSONObject value = array.getJSONObject(key);
			Stage stage = new Stage();
			try {
				stage.setStageId(Integer.valueOf(key));
			} catch (Exception e) {
				Log.log(0, "新增");
			}
			
			Project project = new Project();
			project.setProjectId(value.getInt("projectId"));
			stage.setProject(project);
			
			SubProject subProject = new SubProject();
			subProject.setSubProjectId(value.getInt("subProjectId"));
			stage.setSubProject(subProject);
			
			StageName stageName = new StageName();
			stageName.setStageNameId(value.getInt("stageNameId"));
			stage.setStageName(stageName);
			
			stage.setPsName(value.getString("psName"));
			
			String planFromDate = value.getString("planFromDate");
			if(StringUtils.isNotEmpty(planFromDate)) {
				stage.setPlanFromDate(DateUtil.strToDate(planFromDate, "yyyy-MM-dd"));
			}
			
			String planToDate = value.getString("planToDate");
			if(StringUtils.isNotEmpty(planToDate)) {
				stage.setPlanToDate(DateUtil.strToDate(planToDate, "yyyy-MM-dd"));
			}
			
			String stageLogId = value.getString("stageLogId");
			StageLog stageLog = new StageLog();
			if(StringUtils.isNotEmpty(stageLogId)) {/
				stageLog.setStageLogId(Integer.valueOf(stageLogId));
				stage.setStageLog(stageLog);
			} else {
				this.setNewStage("1");
			}
			
			/*String departmentIds = value.getString("departmentIds");
			stage.setDepartmentIds(departmentIds);*/
			stageList.add(stage);
		}
		return stageList;
	}
 

 

 

 

 

 

你可能感兴趣的:(java)