js判断json是否存在某个对象,python判断json中是否存在某个key
00-1010判断jsonObject中是否有key,检查JSON字符串中是否存在Key。下面是我自己的情况。
目录
JSON object JSON obj=new JSON object();jsonObj.put(版本, 1 . 0 . 0 );//版本号jsonObj.put(编码, UTF-8 );//编码方法确定jsonObject是否具有vesion属性。
JSON obj . has( version );//返回true
00-1010初衷,由于工作需要,两家公司需要接口数据对接,公司一需要向公司二发送json数据串。但是公司一发来的json数据中的字段是不确定的。听听他们的解释,客户在系统上输入的时候会把那些字段带过来,然后客户不输入的那些字段就默认不输入了。公司1要求公司2,未检测到的字段默认值为空!
所以作为单元2,我们需要想办法检测单元1的哪些字段没有带到这里。一开始想到的是使用异常处理,因为程序本身在测试的时候就被报告没有找到,所以使用异常处理也情有可原。如果使用异常,在认为只能捕捉未发现后,直接清空finally中的已捕捉字段就可以了。
那么问题来了,试试异常情况,最后处理的太多了;于是想到了用另外一种方法,用一个json containsKey()函数来判断这个键在json字符串中是否存在,也就是这个字段是否存在;当然还有其他可以判断的功能。这个是基于我自己介绍的json包。
00-1010导入的jar包是:
我首先导入的json包:
导入net . SF . JSON . JSON array;导入net . SF . JSON . JSON object;代码如下:
/** * 9903接口解析json数据放入仓库* * @ author syp * @ time 2019年7月3日11336049:02 */public string date repository(string JSON data){ log4j bean . logger . info(开始处理入库程序!);读取配置。PullConfigXml();//启动配置文件DBUtils db=new DBUtils();JSON object JSON=new JSON object();string JSON str=JSON data . substring(44,JSON data . length());JSON object JSON object=JSON object . from object(JSON str);JSON object JSON card=JSON object . getjson object( cardid _ info )JSON object JSON obu=JSON object . getjson object( obu _ info );JSON object JSON user=JSON object . getjson object( user _ info );JSON object JSON car=JSON object . getjson object( car _ info );//准备将所有json数据信息放入开卡集合表etc _ open _ card _ collection JSON object JSON accno=JSON object . getjson object( accno _ info );字符串all SQL= insert into ETC _ OPEN _ CARD _ COLLECTION(account ID,LINKMOBILE,ACCNO_USERNAME,CERTSN,POSID,GENTIME,TRX_SERNO,EMPLOYEEID,ORG_TRX_SERNO,CARD_ID,CARDTYPE,CARD_BRAND,CARD_MODEL,AGENCYID,CARD_USERID,VEHICLEID,ENABLETIME,EXPIRETIME,ISSUEDTYPE,CHANNELID,ISSUEDTIME,CARD_STATUS,STATUSCHANGETIME,CARD_OPERATION,OBU_ID,OBU_BRAND,OBU_MODEL,OBU _ user
LTYPE,INSTALLCHANNELID,INSTALLTIME,OBU_STATUS,OBU_STATUSCHANGETIME,OBU_OPERATION,USER_ID,USERTYPE,USER_NAME,USERIDTYPE,USERIDNUM,USER_TEL,USER_ADDRESS,USER_REGISTEREDTYPE,USER_CHANNELID,USER_REGISTEREDTIME,DEPARTMENT,AGENTNAME,AGENTIDTYPE,AGENTIDNUM,USER_STATUS,USER_STATUSCHANGETIME,USER_OPERATION,CAR_ID,CAR_TYPE,CAR_USERID,OWNERNAME,OWNERIDTYPE,OWNERIDNUM,OWNERTEL,CAR_ADDRESS,CAR_CONTACT,CAR_REGISTEREDTYPE,CAR_CHANNELID,CAR_REGISTEREDTIME,VEHICLETYPE,VEHICLEMODEL,USECHARACTER,VIN,ENGINENUM,REGISTERDATE,ISSUEDATE,FILENUM,APPROVEDCOUNT,TOTALMASS,MAINTENANCEMASS,PERMITTEDWEIGHT,OUTSIDEDIMENSIONS,PERMITTEDTOWWEIGHT,TESTRECORD,WHEELCOUNT,AXLECOUNT,AXLEDISTANCE,AXISTYPE,CAR_OPERATION) values("+ (jsonAccno.containsKey("accountid")?jsonAccno.getString("accountid"):"")+ ","+ (jsonAccno.containsKey("linkmobile")?jsonAccno.getString("linkmobile"):"") + ","+ (jsonAccno.containsKey("username")?jsonAccno.getString("username"):"") + ","+ (jsonAccno.containsKey("certsn")?jsonAccno.getString("certsn"):"") + ","+ (jsonAccno.containsKey("posid")?jsonAccno.getString("posid"):"")+ ","+ (jsonAccno.containsKey("gentime")?jsonAccno.getString("gentime"):"") + ","+ (jsonAccno.containsKey("trx_serno")?jsonAccno.getString("trx_serno"):"") + ","+ (jsonAccno.containsKey("employeeid")?jsonAccno.getString("employeeid"):"") + ","+ (jsonAccno.containsKey("org_trx_serno")?jsonAccno.getString("org_trx_serno"):"") + ","+ (jsonCard.containsKey("id")?jsonCard.getString("id"):"")+ ","+ (jsonCard.containsKey("cardType")?jsonCard.getString("cardType"):"") + ","+ (jsonCard.containsKey("brand")?jsonCard.getString("brand"):"") + ","+ (jsonCard.containsKey("model")?jsonCard.getString("model"):"") + ","+ (jsonCard.containsKey("agencyId")?jsonCard.getString("agencyId"):"") + ","+ (jsonCard.containsKey("userId")?jsonCard.getString("userId"):"") + ","+ (jsonCard.containsKey("vehicleId")?jsonCard.getString("vehicleId"):"") + ","+ (jsonCard.containsKey("enableTime")?jsonCard.getString("enableTime"):"") + ","+ (jsonCard.containsKey("expireTime")?jsonCard.getString("expireTime"):"") + ","+ (jsonCard.containsKey("issuedType")?jsonCard.getString("issuedType"):"") + ","+ (jsonCard.containsKey("channelId")?jsonCard.getString("channelId"):"") + ","+ (jsonCard.containsKey("issuedTime")?jsonCard.getString("issuedTime"):"") + ","+ (jsonCard.containsKey("status")?jsonCard.getString("status"):"") + ","+ (jsonCard.containsKey("statusChangeTime")?jsonCard.getString("statusChangeTime"):"")+ ","+ (jsonCard.containsKey("operation")?jsonCard.getString("operation"):"")+ ","+ (jsonObu.containsKey("id")?jsonObu.getString("id"):"")+ ","+ (jsonObu.containsKey("brand")?jsonObu.getString("brand"):"")+ ","+ (jsonObu.containsKey("model")?jsonObu.getString("model"):"")+ ","+ (jsonObu.containsKey("userId")?jsonObu.getString("userId"):"")+ ","+ (jsonObu.containsKey("vehicleId")?jsonObu.getString("vehicleId"):"")+ ","+ (jsonObu.containsKey("enableTime")?jsonObu.getString("enableTime"):"")+ ","+ (jsonObu.containsKey("expireTime")?jsonObu.getString("expireTime"):"")+ ","+ (jsonObu.containsKey("registeredType")?jsonObu.getString("registeredType"):"")+ ","+ (jsonObu.containsKey("registeredChannelId")?jsonObu.getString("registeredChannelId"):"")+ ","+ (jsonObu.containsKey("registeredTime")?jsonObu.getString("registeredTime"):"")+ ","+ (jsonObu.containsKey("installType")?jsonObu.getString("installType"):"")+ ","+ (jsonObu.containsKey("installChannelId")?jsonObu.getString("installChannelId"):"")+ ","+ (jsonObu.containsKey("installTime")?jsonObu.getString("installTime"):"")+ ","+ (jsonObu.containsKey("status")?jsonObu.getString("status"):"")+ ","+ (jsonObu.containsKey("statusChangeTime")?jsonObu.getString("statusChangeTime"):"")+ ","+ (jsonObu.containsKey("operation")?jsonObu.getString("operation"):"")+ ","+ (jsonUser.containsKey("id")?jsonUser.getString("id"):"")+ "," + (jsonUser.containsKey("userType")?jsonUser.getString("userType"):"") + "," + (jsonUser.containsKey("userName")?jsonUser.getString("userName"):"") + "," + (jsonUser.containsKey("userIdType")?jsonUser.getString("userIdType"):"") + "," + (jsonUser.containsKey("userIdNum")?jsonUser.getString("userIdNum"):"") + "," + (jsonUser.containsKey("tel")?jsonUser.getString("tel"):"") + "," + (jsonUser.containsKey("address")?jsonUser.getString("address"):"") + "," + (jsonUser.containsKey("registeredType")?jsonUser.getString("registeredType"):"") + "," + (jsonUser.containsKey("channelId")?jsonUser.getString("channelId"):"") + "," + (jsonUser.containsKey("registeredTime")?jsonUser.getString("registeredTime"):"") + "," + (jsonUser.containsKey("department")?jsonUser.getString("department"):"") + "," + (jsonUser.containsKey("agentName")?jsonUser.getString("agentName"):"") + "," + (jsonUser.containsKey("agentIdType")?jsonUser.getString("agentIdType"):"") + "," + (jsonUser.containsKey("agentIdNum")?jsonUser.getString("agentIdNum"):"") + "," + (jsonUser.containsKey("status")?jsonUser.getString("status"):"") + "," + (jsonUser.containsKey("statusChangeTime")?jsonUser.getString("statusChangeTime"):"") + "," + (jsonUser.containsKey("operation")?jsonUser.getString("operation"):"")+ "," + (jsonCar.containsKey("id")?jsonCar.getString("id"):"") + ","+ (jsonCar.containsKey("type")?jsonCar.getString("type"):"") + ","+ (jsonCar.containsKey("userId")?jsonCar.getString("userId"):"") + ","+(jsonCar.containsKey("ownerName")?jsonCar.getString("ownerName"):"") + ","+(jsonCar.containsKey("ownerIdType")?jsonCar.getString("ownerIdType"):"") + ","+(jsonCar.containsKey("ownerIdNum")?jsonCar.getString("ownerIdNum"):"") + ","+(jsonCar.containsKey("ownerTel")?jsonCar.getString("ownerTel"):"") + ","+(jsonCar.containsKey("address")?jsonCar.getString("address"):"") + ","+(jsonCar.containsKey("contact")?jsonCar.getString("contact"):"") + ","+(jsonCar.containsKey("registeredType")?jsonCar.getString("registeredType"):"") + ","+(jsonCar.containsKey("channelId")?jsonCar.getString("channelId"):"") + ","+(jsonCar.containsKey("registeredTime")?jsonCar.getString("registeredTime"):"") + ","+(jsonCar.containsKey("vehicleType")?jsonCar.getString("vehicleType"):"") + ","+(jsonCar.containsKey("vehicleModel")?jsonCar.getString("vehicleModel"):"") + ","+(jsonCar.containsKey("useCharacter")?jsonCar.getString("useCharacter"):"") + ","+(jsonCar.containsKey("VIN")?jsonCar.getString("VIN"):"") + ","+(jsonCar.containsKey("engineNum")?jsonCar.getString("engineNum"):"") + ","+(jsonCar.containsKey("registerDate")?jsonCar.getString("registerDate"):"") + ","+(jsonCar.containsKey("issueDate")?jsonCar.getString("issueDate"):"") + ","+(jsonCar.containsKey("fileNum")?jsonCar.getString("fileNum"):"") + ","+(jsonCar.containsKey("approvedCount")?jsonCar.getString("approvedCount"):"") + ","+(jsonCar.containsKey("totalMass")?jsonCar.getString("totalMass"):"") + ","+(jsonCar.containsKey("maintenanceMass")?jsonCar.getString("maintenanceMass"):"") + ","+(jsonCar.containsKey("permittedWeight")?jsonCar.getString("permittedWeight"):"") + ","+(jsonCar.containsKey("outsideDimensions")?jsonCar.getString("outsideDimensions"):"") + ","+(jsonCar.containsKey("permittedTowWeight")?jsonCar.getString("permittedTowWeight"):"") + ","+(jsonCar.containsKey("testRecord")?jsonCar.getString("testRecord"):"") + ","+(jsonCar.containsKey("wheelCount")?jsonCar.getString("wheelCount"):"") + ","+(jsonCar.containsKey("axleCount")?jsonCar.getString("axleCount"):"") + ","+(jsonCar.containsKey("axleDistance")?jsonCar.getString("axleDistance"):"") + ","+(jsonCar.containsKey("axisType")?jsonCar.getString("axisType"):"") + ","+(jsonCar.containsKey("operation")?jsonCar.getString("operation"):"") + ")";int allParam = db.updateMethod(allSql, null);if (allParam > 0) {Log4jBean.logger.info("开卡集合数据写入开卡集合表成功!");json.put("return_msg", "处理成功!");json.put("return_code", "0");} else {Log4jBean.logger.error("开卡集合数据写入开卡集合表失败!");json.put("return_msg", "处理失败!");json.put("return_code", "-1");}return json.toString();}通过代码可以看出,每个字段都用containsKey()函数做了处理,这样就不用害怕单位一发过来的j'son串中哪个字段找不到的情况了;
最后经测试已成功入库。。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。