English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When doing a project, I encountered such a pit: how to judge whether a json is empty,
The initial code was like this:
JSONObject detail = JSONObject.fromObject("123"); if (detail == null) { detail = new JSONObject(); }
Found a problem, no matter how to adjust the content of the string, it will not go to new Jsonobject(). Try various methods, ask various questions, and finally there is no way to try the methods of detail one by one, and it turns out that there is an isNullObject() method, so the following code came out,
JSONObject detail = JSONObject.fromObject("123"); if (detail == null || detail.isNullObject()) { detail = new JSONObject(); }
After so long, all kinds of pitfalls have been encountered, and there will still be pitfalls in the future,Solution:
1. Search online to see if there are similar problems;
2. In situations like today, the class may have provided available methods, try some of the methods;
3. Colleagues are good teachers, ask more, face value is the least valuable here.
The above net.sf.json.JSONObject is null judgment method is all the editor shared with everyone, hoping to give everyone a reference, and also hope everyone will support the Shouting Tutorial more.