public static void main(String[] args) { String data = "{"code":null,"msg":null,"data":null,"opcode":"1","reason":"成功","result":{"firstGameData":null,"playerGameAccount":"zero","lastGameData":null}}"; JSONObject jsonResult = JSONObject.fromObject(data); JSONObject result = jsonResult.getJSONObject("result");json
System.out.println(result); System.out.println(result.getJSONObject("firstGameData")); System.out.println(result.getJSONObject("firstGameData").isNullObject()); }
解析结果: {"firstGameData":null,"playerGameAccount":"zero","lastGameData":null} null true 解决办法: JsonObject有一个方法是判断value是否为空,JsonObject.isNull(key)。解析前使用此方法判断下json对象便可。code