写法和 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")); } }
相关推荐
1.解析json字符串 2.将json字符串转换为java对象 3.将java对象转换为json字符串
org.json资源包,json中的工具jar包,使用此包(org.json)解析Json.
commons-beanutils-1.9.3.jar、commons-collections-3.2.2.jar、commons-lang-2.6.jar、commons-logging-1.2.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
文档附件用于阐述net.sf.json.JSONObject对象的使用,详细讲述了存储和检索,对于Java Web开发和Spring-MVC的很有帮助
commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
有个哥们10分要,我就看不惯了,2分要的拿去,本来想免费的结果最少要2分不好意思了各位
Json解析 Nii.JSON.dll,Json解析 Nii.JSON.dll,Json解析 Nii.JSON.dll JSONObject jo = new JSONObject(); var json = jo.ConvertToJSONObject(text);
net.sf.json jar包包含了一下:可以用来解析json,具体操作可以看小编的博客。 commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
用来解析json格式的,里面有commons-beanutils-1.9.3.jar commons-collections-3.2.2.jar commons-lang-2.6.jar commons-logging-1.2.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar
3种JSON解析包,fastjson alibaba jar包,org.json.JSONObject包,net.sf.json.JSONObject包,齐全包
解析器输出与org.json.JSONObject.wrap兼容,从而允许将输出轻松转换为org.json对象。 这允许在为org.json类型编写的应用程序中使用JSON5文件; 允许注释,未加引号的键,单引号的字符串,字符串中的行继续,十六...
方便好用的json解析库,每个json元素都是一个JsonElement对象,通过这个对象,你可以很方便的从Json子类对象中得到你想要的值,不需要你进一步转换,用法如: JsonObject jsonObject = new JsonObject (...); 在...
教你如何连接数据库解析Kettle中的json串
Newtonsoft.Json已经足够强大,易用性也没得说。但是在使用过程中存在着一个问题,如果数据不存在...JsonObject类和JsonArray类可以相互嵌套,支持原生类的JsonPath检索能力,满足Json数据解析和Json数据封装常见需求。
组装和解析JSONObject的Json字符串 组装和解析JSONObject的Json字符串
C#的一个解析JSON的工具类,像java一样优雅的解析JSON数据,包含有JSONObject和JSONArray对象。 JSONObject json = new JSONObject(); json.Put("sex", "男"); json.Put("age", 123); json.Put("name", "张三"); ...
json解析jar包,6个jar包都是必须的,引入jar包后导入import net.sf.json.JSONArray; import net.sf.json.JSONObject;可对json和jsonArray解析.
目的:当App中用到固定的json数据时,如:国家城市列表、班级成员等时,可以将json数据制作为本地json文件存入assets文件夹下。 步骤如下: 1、新建assets文件夹,并将json文件复制到此文件夹中 在main文件夹下新建...
JAVA在线接收解析JSON数据,引用的JSON类为 net.sf.json.JSONArray; net.sf.json.JSONObject;