获取json路径下的某个元素(如果路径不存在返回null值,并自动创建路径) @param jsonPath 例如"rpg/man/hp",rpg和man为jsonObject @return
(String jsonPath)
| 78 | * @return |
| 79 | */ |
| 80 | public Object getIt(String jsonPath) { |
| 81 | jsonPath = jsonPath.replaceAll("\\.", "/"); |
| 82 | String[] paths = jsonPath.split("/"); |
| 83 | Object o = null; |
| 84 | JSONObject object = jsonObj; |
| 85 | try { |
| 86 | for (String name : paths) { |
| 87 | if (object.isNull(name)) |
| 88 | object.put(name, new JSONObject()); |
| 89 | o = object.get(name); |
| 90 | if (o instanceof JSONObject) |
| 91 | object = (JSONObject) o; |
| 92 | } |
| 93 | } catch (JSONException e) { |
| 94 | e.printStackTrace(); |
| 95 | } |
| 96 | return o; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * 新增,修改,删除(修改所在路径的值,如果不存在则自动创建路径。如果值为null则为删除) |