|
17 | 17 | import static zuo.biao.apijson.JSONObject.KEY_COMBINE;
|
18 | 18 | import static zuo.biao.apijson.JSONObject.KEY_DROP;
|
19 | 19 | import static zuo.biao.apijson.JSONObject.KEY_TRY;
|
| 20 | +import static zuo.biao.apijson.RequestMethod.POST; |
20 | 21 | import static zuo.biao.apijson.RequestMethod.PUT;
|
21 | 22 | import static zuo.biao.apijson.server.SQLConfig.TYPE_ITEM;
|
22 | 23 |
|
@@ -240,8 +241,33 @@ public AbstractObjectParser parse() throws Exception {
|
240 | 241 | response.put(key, onChildParse(index, key, (JSONObject)value));
|
241 | 242 | index ++;
|
242 | 243 | }
|
243 |
| - } |
244 |
| - else if (method == PUT && value instanceof JSONArray |
| 244 | + } else if (value instanceof JSONArray && method == POST && |
| 245 | + key.startsWith("@") == false && key.endsWith("@") == false) {//JSONArray,批量新增,往下一级提取 |
| 246 | + JSONArray valueArray = (JSONArray)value; |
| 247 | + |
| 248 | + for (int i = 0; i < valueArray.size(); i++) { |
| 249 | + if (childMap != null) {//添加到childMap,最后再解析 |
| 250 | + childMap.put(key, valueArray.getJSONObject(i)); |
| 251 | + } |
| 252 | + else {//直接解析并替换原来的,[]:{} 内必须直接解析,否则会因为丢掉count等属性,并且total@:"/[]/total"必须在[]:{} 后! |
| 253 | + JSONObject result = (JSONObject)onChildParse(index, key, valueArray.getJSONObject(i)); |
| 254 | + //合并结果 |
| 255 | + JSONObject before = (JSONObject)response.get(key); |
| 256 | + if(result.get("code").equals(200)){ |
| 257 | + if(before!=null){ |
| 258 | + before.put("count",before.getInteger("count")+result.getInteger("count")); |
| 259 | + response.put(key, before); |
| 260 | + }else{ |
| 261 | + response.put(key, result); |
| 262 | + } |
| 263 | + } else { |
| 264 | + //只要有一条失败,则抛出异常,全部失败 |
| 265 | + throw new RuntimeException(key + "," + valueArray.getJSONObject(i) +",新增失败!"); |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + index ++; |
| 270 | + } else if (method == PUT && value instanceof JSONArray |
245 | 271 | && (whereList == null || whereList.contains(key) == false)) {//PUT JSONArray
|
246 | 272 | onPUTArrayParse(key, (JSONArray) value);
|
247 | 273 | }
|
|
0 commit comments