Commit 77f33c865478e0f2c19506f271e54dccd3380085
1 parent
c65275db
Exists in
master
optimized jsonutil
Showing
2 changed files
with
37 additions
and
29 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilJSON.java
| ... | ... | @@ -6,6 +6,19 @@ import net.sf.json.JSONArray; |
| 6 | 6 | import net.sf.json.JSONObject; |
| 7 | 7 | |
| 8 | 8 | public class UtilJSON { |
| 9 | + public static boolean isJsonNull(Object data){ | |
| 10 | + if(data == null){ | |
| 11 | + return true; | |
| 12 | + } | |
| 13 | + if(!(data instanceof String) && data.toString().equals("null")){ | |
| 14 | + return true; | |
| 15 | + } | |
| 16 | + if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){ | |
| 17 | + return true; | |
| 18 | + } | |
| 19 | + return false; | |
| 20 | + } | |
| 21 | + | |
| 9 | 22 | public static void removeJsonNull(JSONObject data){ |
| 10 | 23 | if(data == null || data.isNullObject()){ |
| 11 | 24 | return; |
| ... | ... | @@ -16,44 +29,35 @@ public class UtilJSON { |
| 16 | 29 | Object value = data.get(keyItem); |
| 17 | 30 | |
| 18 | 31 | if(isJsonNull(value)) { |
| 19 | - data.put(keyItem, ""); | |
| 32 | + data.remove(keyItem); | |
| 33 | + continue; | |
| 20 | 34 | } |
| 21 | 35 | |
| 22 | 36 | if(value instanceof JSONObject){ |
| 23 | 37 | removeJsonNull((JSONObject)value); |
| 24 | - continue; | |
| 25 | - } | |
| 26 | - | |
| 27 | - if(value instanceof JSONArray){ | |
| 38 | + }else if(value instanceof JSONArray){ | |
| 28 | 39 | removeJsonNull((JSONArray)value); |
| 29 | 40 | } |
| 30 | 41 | } |
| 31 | 42 | } |
| 32 | - | |
| 33 | - public static boolean isJsonNull(Object data){ | |
| 34 | - if(data == null){ | |
| 35 | - return true; | |
| 36 | - } | |
| 37 | - if(!(data instanceof String) && data.toString().equals("null")){ | |
| 38 | - return true; | |
| 39 | - } | |
| 40 | - if(data instanceof JSONObject && ((JSONObject) data).isNullObject()){ | |
| 41 | - return true; | |
| 42 | - } | |
| 43 | - return false; | |
| 44 | - } | |
| 45 | - | |
| 43 | + | |
| 46 | 44 | public static void removeJsonNull(JSONArray dataArr){ |
| 47 | 45 | if(dataArr == null){ |
| 48 | 46 | return; |
| 49 | 47 | } |
| 50 | 48 | for(int i=0; i<dataArr.size(); ++i){ |
| 51 | - JSONObject dataItem = dataArr.optJSONObject(i); | |
| 49 | + Object dataItem = dataArr.get(i); | |
| 52 | 50 | if(isJsonNull(dataItem)){ |
| 53 | 51 | dataArr.remove(i); |
| 52 | + --i; | |
| 54 | 53 | continue; |
| 55 | 54 | } |
| 56 | - removeJsonNull(dataItem); | |
| 55 | + | |
| 56 | + if(dataItem instanceof JSONObject){ | |
| 57 | + removeJsonNull((JSONObject)dataItem); | |
| 58 | + }else if(dataItem instanceof JSONArray){ | |
| 59 | + removeJsonNull((JSONArray)dataItem); | |
| 60 | + } | |
| 57 | 61 | } |
| 58 | 62 | } |
| 59 | 63 | |
| ... | ... | @@ -68,14 +72,12 @@ public class UtilJSON { |
| 68 | 72 | |
| 69 | 73 | if(isJsonNull(value)) { |
| 70 | 74 | data.put(keyItem, target); |
| 75 | + continue; | |
| 71 | 76 | } |
| 72 | 77 | |
| 73 | 78 | if(value instanceof JSONObject){ |
| 74 | 79 | replaceJsonNull((JSONObject)value, target); |
| 75 | - continue; | |
| 76 | - } | |
| 77 | - | |
| 78 | - if(value instanceof JSONArray){ | |
| 80 | + }else if(value instanceof JSONArray){ | |
| 79 | 81 | replaceJsonNull((JSONArray)value, target); |
| 80 | 82 | } |
| 81 | 83 | } |
| ... | ... | @@ -86,12 +88,18 @@ public class UtilJSON { |
| 86 | 88 | return; |
| 87 | 89 | } |
| 88 | 90 | for(int i=0; i<dataArr.size(); ++i){ |
| 89 | - JSONObject dataItem = dataArr.optJSONObject(i); | |
| 91 | + Object dataItem = dataArr.get(i); | |
| 90 | 92 | if(isJsonNull(dataItem)){ |
| 91 | - dataArr.set(i, target); | |
| 93 | + dataArr.remove(i); | |
| 94 | + --i; | |
| 92 | 95 | continue; |
| 93 | 96 | } |
| 94 | - removeJsonNull(dataItem); | |
| 97 | + | |
| 98 | + if(dataItem instanceof JSONObject){ | |
| 99 | + replaceJsonNull((JSONObject)dataItem, target); | |
| 100 | + }else if(dataItem instanceof JSONArray){ | |
| 101 | + replaceJsonNull((JSONArray)dataItem, target); | |
| 102 | + } | |
| 95 | 103 | } |
| 96 | 104 | } |
| 97 | 105 | ... | ... |