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