jsonObject的一些方法
1.从前端传过来的数字,默认是Integer类型不能直接用Long接收
错误写法:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。报错:Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Long demendid=(Long)jsonObject.get("demandId");
正确写法:
Long demendid=((Integer)jsonObject.get("demandId")).longValue();
Long demendidd=((Number)jsonObject.get("demandId")).longValue();
最简便的:
Long demandId=jsonObject.optLong("demandId");
与jsonObject.optLong()类似还有
jsonObject.optInteger()
jsonObject.optInt()
jsonObject.optBoolean()
jsonObject.optString()
jsonObject.optJSONArray()
转换为对象
Demand demand = (Demand) JSONObject.toBean(demandJson, Demand.class);

更多精彩