`

org.json.JSONObject对json的解析

阅读更多

 

 

 

写法和 google的json类解析json类似, 贴出来做一个备份:

 

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xuele.bigdata.xetl.model.ActionLogInfo;
import com.xuele.bigdata.xetl.model.Constant;


public class JsonParseUtil {

	private static Logger logger= LoggerFactory.getLogger(JsonParseUtil.class);
	private static String dataObjKey = "data";

	public static ActionLogInfo parseActionJson(String jsonStr){
		
		try {
			JSONObject jsonObj;
			try {
				jsonObj = new JSONObject(jsonStr);
			} catch (JSONException e) {
				logger.error("----"+jsonStr);
				logger.error("", e);
				return null;
			}
			int actId=jsonObj.getInt(Constant.ACTID);
			String module="";
			if(jsonObj.has(Constant.MODEL)){
				module=jsonObj.getString(Constant.MODEL);
			}
			String ip="";
			if(jsonObj.has(Constant.IP)){
				ip=jsonObj.getString(Constant.IP);
			}
			String countyId="";
			if(jsonObj.has(Constant.COUNTYID)){
				countyId=jsonObj.getString(Constant.COUNTYID);
			}
			String countyName="";
			if(jsonObj.has(Constant.COUNTYNAME)){
				countyName=jsonObj.getString(Constant.COUNTYNAME);
			}
			int originCode=jsonObj.getInt(Constant.ORIGINCODE);
			String role=jsonObj.getString(Constant.ROLE);
			String schoolId="";
			if(jsonObj.has(Constant.SCHOOLID)){
				schoolId=jsonObj.getString(Constant.SCHOOLID);
			}
			String schoolName="";
			if(jsonObj.has(Constant.SCHOOLNAME)){
				schoolName=jsonObj.getString(Constant.SCHOOLNAME);
			}
			long time=0;
			if(jsonObj.has(Constant.TIME)){
				time=jsonObj.getLong(Constant.TIME);
			}
			//String userIcon=jsonObj.getString(Constant.USERICON);
			String userIcon="";
			if(jsonObj.has(Constant.USERICON))
				userIcon=jsonObj.getString(Constant.USERICON);

			String userId=jsonObj.getString(Constant.USERID);
			String userIp=jsonObj.getString(Constant.USERIP);
			String userName="";
			if(jsonObj.has(Constant.USERNAME)){
				userName=jsonObj.getString(Constant.USERNAME);
			}
			String provinceId="";
			if(jsonObj.has(Constant.PROVINCEID)){
				provinceId=jsonObj.getString(Constant.PROVINCEID);
			}
			String provinceName="";
			if(jsonObj.has(Constant.PROVINCENAME)){
				provinceName=jsonObj.getString(Constant.PROVINCENAME);
			}
			String cityId="";
			if(jsonObj.has(Constant.CITYID)){
				cityId=jsonObj.getString(Constant.CITYID);
			}
			String cityName="";
			if(jsonObj.has(Constant.CITYNAME)){
				cityName=jsonObj.getString(Constant.CITYNAME);
			}
			//新增班级ID,班级名称,年级ID ADD BY WHH 2017-05-26
			String className = "";
			if(jsonObj.has(Constant.CLASSNAME)){
				className=jsonObj.getString(Constant.CLASSNAME);
			}
			String classId = "";
			if(jsonObj.has(Constant.CLASSID)){
				classId=jsonObj.getString(Constant.CLASSID);
			}

			String gradeId = "";
			if(jsonObj.has(Constant.GRADEID)){
				gradeId=jsonObj.getString(Constant.GRADEID);
			}





			JSONObject dataObj=jsonObj.getJSONObject(dataObjKey);

			String postId="";
			if(dataObj.has(Constant.POSTID)){
				postId=dataObj.getString(Constant.POSTID);
			}
			
			int postType=0;
			if(dataObj.has(Constant.POSTTYPE)){
//			postType=dataObj.getString(Constant.POSTTYPE);
				if(StringUtils.isEmpty(dataObj.get(Constant.POSTTYPE).toString())){
					postType=-1;
				}else{
					postType=Integer.parseInt(dataObj.get(Constant.POSTTYPE).toString());
				}
			}
			
			String beUserId = "";
//			if(dataObj.has("praiseUserId")){//praisedUserId
//				beUserId=dataObj.get("praiseUserId").toString();
//			}else 
			if(dataObj.has("beCommentUserId")){//beCommentUserId
				beUserId=dataObj.get("beCommentUserId").toString();
			}else if(dataObj.has("praisedUserId")){
				beUserId=dataObj.getString("praisedUserId");
			}
			
			String beUserName = "";
			if(dataObj.has("praisedUserName")){//praisedUserName
				beUserName=dataObj.get("praisedUserName").toString();
			}else if(dataObj.has("beCommentUserName")){//beCommentUserName
				beUserName=dataObj.get("beCommentUserName").toString();
			}
			
			String beUserRoleId = "";
			if(dataObj.has("praisedUserRoleId")){
				beUserRoleId=dataObj.get("praisedUserRoleId").toString();
			}else if(dataObj.has("beCommentUserRoleId")){
				beUserRoleId=dataObj.get("beCommentUserRoleId").toString();
			}
			String beUserIcon = "";
			if(dataObj.has("praisedUserIcon")){
				beUserIcon=dataObj.get("praisedUserIcon").toString();
			}else if(dataObj.has("beCommentUserIcon")){
				beUserIcon=dataObj.get("beCommentUserIcon").toString();
			}
			String beUserCountyId = "";
			if(dataObj.has("praisedUserCountyId")){
				beUserCountyId=dataObj.getString("praisedUserCountyId");
			}else if(dataObj.has("beCommentUserCountyId")){
				beUserCountyId=dataObj.getString("beCommentUserCountyId");
			}
			
			String subjectId = "";
			if(dataObj.has(Constant.SUBJECTID)){
				subjectId=dataObj.getString(Constant.SUBJECTID);
			}
			String subjectName = "";
			if(dataObj.has(Constant.SUBJECTNAME)){
				subjectName=dataObj.getString(Constant.SUBJECTNAME);
			}
			
			String unitId = "";
			if(dataObj.has(Constant.UNITID)){
				unitId=dataObj.getString(Constant.UNITID);
			}
			String unitName = "";
			if(dataObj.has(Constant.COUNTYNAME)){
				unitName=dataObj.getString(Constant.UNITNAME);
			}
			int teachType = 0;
			if(dataObj.has(Constant.TEACHERTYPE)){
				teachType=dataObj.getInt(Constant.TEACHERTYPE);
			}
			int teachChooseStudentCount = 0;
			if(dataObj.has(Constant.TEACHCHOOSESTUDENTCOUNT)){
				teachChooseStudentCount=dataObj.getInt(Constant.TEACHCHOOSESTUDENTCOUNT);
			}
			
			//1600022 作业点赞   发起人 (userId)为被统计学生

			//1600035  提交作业  发起人 (userId)为被统计学生
			String bookId = "";
			if(dataObj.has(Constant.BOOKID)){
				bookId=dataObj.getString(Constant.BOOKID);
			}
			//奥数提分宝:确认交卷  ID  1713007(无)
			String challengeId = "";
			if(dataObj.has(Constant.CHALLENGEID)){
				challengeId=dataObj.getString(Constant.CHALLENGEID);
			}
			int challengeRightQuestionCount = 0;
			if(dataObj.has(Constant.CHALLENGERIGHTQUESTIONCOUNT)){
//			challengeRightQuestionCount=dataObj.getString(Constant.CHALLENGERIGHTQUESTIONCOUNT);
				challengeRightQuestionCount=dataObj.getInt(Constant.CHALLENGERIGHTQUESTIONCOUNT);
			}
			int challengeQuestionCount = 0;
			if(dataObj.has(Constant.CHALLENGEQUESTIONCOUNT)){
//			challengeQuestionCount=dataObj.getString(Constant.CHALLENGEQUESTIONCOUNT);
				challengeQuestionCount=dataObj.getInt(Constant.CHALLENGEQUESTIONCOUNT);
			}
			
			String beCommentUserList="";
			if(dataObj.has("beCommentUserList")){
				System.out.println("beCommentUserList:"+beCommentUserList);
				JSONArray jsonArray=dataObj.getJSONArray("beCommentUserList");
				JSONObject jObj=null;
				for(int i=0;i<jsonArray.length();i++){
					jObj=jsonArray.getJSONObject(i);
					if(jObj.has("beCommentUserId")){
						beCommentUserList+=jObj.getString("beCommentUserId");
						if(i<jsonArray.length()){
							beCommentUserList+="\002";
						}
					}
				}
			}
			
			ActionLogInfo alInfo=new ActionLogInfo();
			alInfo.setActId(actId);
			alInfo.setBeUserId(beUserId);
			alInfo.setBeUserCountyId(beUserCountyId);
			alInfo.setBeUserIcon(beUserIcon);
			alInfo.setBeUserName(beUserName);
			alInfo.setBeUserRoleId(beUserRoleId);
			alInfo.setBookId(bookId);
			alInfo.setChallengeId(challengeId);
			alInfo.setChallengeQuestionCount(challengeQuestionCount);
			alInfo.setChallengeRightQuestionCount(challengeRightQuestionCount);
			alInfo.setCityId(cityId);
			alInfo.setCityName(cityName);
			alInfo.setClassId(classId);
			alInfo.setClassName(className);
			alInfo.setGradeId(gradeId);
			alInfo.setCountyId(countyId);
			alInfo.setCountyName(countyName);
			alInfo.setIp(ip);
			alInfo.setModule(module);
			alInfo.setOriginCode(originCode);
			alInfo.setPostId(postId);
			alInfo.setPostType(postType);
			alInfo.setProvinceId(provinceId);
			alInfo.setProvinceName(provinceName);
			alInfo.setRole(role);
			alInfo.setSchoolId(schoolId);
			alInfo.setSchoolName(schoolName);
			alInfo.setSubjectId(subjectId);
			alInfo.setSubjectName(subjectName);
			alInfo.setTeachChooseStudentCount(teachChooseStudentCount);
			alInfo.setTeachType(teachType);
			alInfo.setTime(time);
			alInfo.setUnitId(unitId);
			alInfo.setUnitName(unitName);
			alInfo.setUserIcon(userIcon);
			alInfo.setUserId(userId);
			alInfo.setUserIp(userIp);
			alInfo.setUserName(userName);
			alInfo.setBeCommentUserList(beCommentUserList);
			
			return alInfo;
		} catch (NumberFormatException e) {
			logger.error("", e);
		} catch (JSONException e) {
			logger.error("", e);
		}
		return null;
		
	}
	
	public static void main(String[] args) {
		JSONObject jobj = new JSONObject();
		System.out.println(jobj.getString("a"));
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics