@@ -309,6 +309,62 @@ public static String getString(String jsonData, String key, String defaultValue)
309
309
}
310
310
}
311
311
312
+ /**
313
+ * get String from jsonObject
314
+ *
315
+ * @param jsonObject
316
+ * @param defaultValue
317
+ * @param keyArray
318
+ * @return <ul>
319
+ * <li>if jsonObject is null, return defaultValue</li>
320
+ * <li>if keyArray is null or empty, return defaultValue</li>
321
+ * <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
322
+ * null, return directly</li>
323
+ * </ul>
324
+ */
325
+ public static String getStringCascade (JSONObject jsonObject , String defaultValue , String ... keyArray ) {
326
+ if (jsonObject == null || ArrayUtils .isEmpty (keyArray )) {
327
+ return defaultValue ;
328
+ }
329
+
330
+ String data = jsonObject .toString ();
331
+ for (String key : keyArray ) {
332
+ data = getStringCascade (data , key , defaultValue );
333
+ if (data == null ) {
334
+ return defaultValue ;
335
+ }
336
+ }
337
+ return data ;
338
+ }
339
+
340
+ /**
341
+ * get String from jsonData
342
+ *
343
+ * @param jsonData
344
+ * @param defaultValue
345
+ * @param keyArray
346
+ * @return <ul>
347
+ * <li>if jsonData is null, return defaultValue</li>
348
+ * <li>if keyArray is null or empty, return defaultValue</li>
349
+ * <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
350
+ * null, return directly</li>
351
+ * </ul>
352
+ */
353
+ public static String getStringCascade (String jsonData , String defaultValue , String ... keyArray ) {
354
+ if (StringUtils .isEmpty (jsonData )) {
355
+ return defaultValue ;
356
+ }
357
+
358
+ String data = jsonData ;
359
+ for (String key : keyArray ) {
360
+ data = getString (data , key , defaultValue );
361
+ if (data == null ) {
362
+ return defaultValue ;
363
+ }
364
+ }
365
+ return data ;
366
+ }
367
+
312
368
/**
313
369
* get String array from jsonObject
314
370
*
@@ -474,7 +530,7 @@ public static JSONObject getJSONObject(JSONObject jsonObject, String key, JSONOb
474
530
* @param key
475
531
* @param defaultValue
476
532
* @return <ul>
477
- * <li>if jsonObject is null, return defaultValue</li>
533
+ * <li>if jsonData is null, return defaultValue</li>
478
534
* <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
479
535
* <li>return {@link JSONUtils#getJSONObject(JSONObject, String, JSONObject)}</li>
480
536
* </ul>
@@ -495,6 +551,63 @@ public static JSONObject getJSONObject(String jsonData, String key, JSONObject d
495
551
}
496
552
}
497
553
554
+ /**
555
+ * get JSONObject from jsonObject
556
+ *
557
+ * @param jsonObject
558
+ * @param defaultValue
559
+ * @param keyArray
560
+ * @return <ul>
561
+ * <li>if jsonObject is null, return defaultValue</li>
562
+ * <li>if keyArray is null or empty, return defaultValue</li>
563
+ * <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
564
+ * null, return directly</li>
565
+ * </ul>
566
+ */
567
+ public static JSONObject getJSONObjectCascade (JSONObject jsonObject , JSONObject defaultValue , String ... keyArray ) {
568
+ if (jsonObject == null || ArrayUtils .isEmpty (keyArray )) {
569
+ return defaultValue ;
570
+ }
571
+
572
+ JSONObject js = jsonObject ;
573
+ for (String key : keyArray ) {
574
+ js = getJSONObject (js , key , defaultValue );
575
+ if (js == null ) {
576
+ return defaultValue ;
577
+ }
578
+ }
579
+ return js ;
580
+ }
581
+
582
+ /**
583
+ * get JSONObject from jsonData
584
+ *
585
+ * @param jsonData
586
+ * @param defaultValue
587
+ * @param keyArray
588
+ * @return <ul>
589
+ * <li>if jsonData is null, return defaultValue</li>
590
+ * <li>if keyArray is null or empty, return defaultValue</li>
591
+ * <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
592
+ * null, return directly</li>
593
+ * </ul>
594
+ */
595
+ public static JSONObject getJSONObjectCascade (String jsonData , JSONObject defaultValue , String ... keyArray ) {
596
+ if (StringUtils .isEmpty (jsonData )) {
597
+ return defaultValue ;
598
+ }
599
+
600
+ try {
601
+ JSONObject jsonObject = new JSONObject (jsonData );
602
+ return getJSONObjectCascade (jsonObject , defaultValue , keyArray );
603
+ } catch (JSONException e ) {
604
+ if (isPrintException ) {
605
+ e .printStackTrace ();
606
+ }
607
+ return defaultValue ;
608
+ }
609
+ }
610
+
498
611
/**
499
612
* get JSONArray from jsonObject
500
613
*
0 commit comments