关于JSONObject取值方法getString()与optString()
Posted 失落的孩子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JSONObject取值方法getString()与optString()相关的知识,希望对你有一定的参考价值。
用JSON这么久,刚发现JSONObject的getString()暗藏炸弹!!!
public Map<String, Object> queryalmMonitorInfo(@RequestBody String jsonStr) { JSONObject json = JSONObject.fromObject(jsonStr); // 当前页码 int currentPage = json.getInt(SysConstant.PAGE); // 每页条数 int pageSize = json.getInt(SysConstant.ROWS); Map<String, Object> mape = new HashMap<String, Object>(); String areaid = json.getString("areaids"); JSONArray areaids = JSONArray.fromObject(areaid); @SuppressWarnings("unchecked") List<String> areaIdlist = (List<String>) JSONArray.toCollection(areaids, String.class); String sysAndeqp = json.getString("eqpLists"); List<String> eqpLists; if ("".equals(sysAndeqp)) { eqpLists = null; } else { JSONArray eqpList = JSONArray.fromObject(sysAndeqp); eqpLists = (List<String>) JSONArray.toCollection(eqpList, String.class); } String sysEmsId = json.getString("sysEmsId"); String techType = json.getString("techType"); List<String> sysEmsIds; if ("".equals(sysEmsId)) { sysEmsIds = null; } else { /* * JSONArray sysEmsIdList = JSONArray.fromObject(sysEmsId); * sysEmsIds = (List<String>) JSONArray.toCollection(sysEmsIdList, * String.class); */ String[] arr = sysEmsId.split(","); sysEmsIds = Arrays.asList(arr); } List<String> techTypes; if ("".equals(techType)) { techTypes = null; } else { /* * JSONArray techTypeList = JSONArray.fromObject(techType); * techTypes = (List<String>) JSONArray.toCollection(techTypeList, * String.class); */ String[] arr1 = techType.split(","); techTypes = Arrays.asList(arr1); } String keyword = json.getString("keyword"); String alarmLevel = json.getString("alarmLevel"); String neEmsId = json.getString("neEmsId"); String shelfEmsId = json.getString("shelfEmsId"); String slotEmsId = json.getString("slotEmsId"); int cardEmsId = json.getInt("cardEmsId"); String isConf = json.getString("isConf"); String portEmsId = json.getString("portEmsId"); PageVo pageVo = PageUtil.assemblePageVo(currentPage, pageSize); mape.put("keyword", keyword); mape.put("alarmLevel", alarmLevel); mape.put("techType", techTypes); mape.put("areaId", areaIdlist); mape.put("sysEmsId", sysEmsIds); mape.put("neEmsId", neEmsId); mape.put("shelfEmsId", shelfEmsId); mape.put("slotEmsId", slotEmsId); mape.put("cardEmsId", cardEmsId); mape.put("portEmsId", portEmsId); mape.put("sysAndeqp", eqpLists); mape.put("isConf", isConf); //新加条件20180628 mape.put("isDamage", json.optString("isDamage")); mape.put("techTypeName", json.optString("techTypeName")); mape.put("sysName", json.optString("sysName")); mape.put("neTypeName", json.optString("neTypeName")); mape.put("alarmLevelOriginal", json.optString("alarmLevelOriginal")); mape.put("alarmNeTimeStar", json.optString("alarmNeTimeStar")); mape.put("alarmNeTimeEnd", json.optString("alarmNeTimeEnd")); mape.put("ruleDesc", json.optString("ruleDesc")); mape.put("projectName", json.optString("projectName")); PageVo resPageVo = almMonitorService.getNetworkScaleAnalysis(pageVo, mape); Map<String, Object> map = new HashMap<String, Object>(); map.put(SysConstant.ROWS, resPageVo.getVoList()); map.put(SysConstant.TOTAL, resPageVo.getRecordCount()); return map; }
项目用的SpringMVC,前端报错经过调试为此方法没有返回值,由于这是别人写的代码并没有try catch(这里表示气愤!!!),经过一点一点debug发现getString()方法无缘无故抛异常了,问了下度娘发现这个方法有雷:当JSONObject中不存在某key的时候就抛异常,而optString()不会,前端写的是自定义条件查询,JSONObject中的key并不是固定不变的,所以这里改为用optString()方法,mybatis配置的sql中有判断,不会影响查询。
以上是关于关于JSONObject取值方法getString()与optString()的主要内容,如果未能解决你的问题,请参考以下文章
JSONObject optString或者getString方法取null问题
接口返回的json中有html标签,<BR>、<p>等,用jsonObject.getString()方法后html标签没了,怎么解决
json三-------com.alibaba.fastjson