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