json类 方便调用

2020-11-15 12:09

阅读:679

标签:des   android   com   http   class   blog   style   img   div   code   java   

jsonutil类

soscw.com,布布扣
  1 package ***********
  2 
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.Type;
  5 import java.util.ArrayList;
  6 import java.util.HashMap;
  7 import java.util.Iterator;
  8 import java.util.List;
  9 import java.util.Map;
 10 
 11 import org.json.JSONArray;
 12 import org.json.JSONException;
 13 import org.json.JSONObject;
 14 
 15 import android.util.Log;
 16 
 17 public class JSONUtil {
 18     public static boolean isPrintException = true;
 19     private static String TAG = "JSONUtil";
 20     private static int modifierPSF = java.lang.reflect.Modifier.FINAL+java.lang.reflect.Modifier.STATIC+java.lang.reflect.Modifier.PUBLIC;
 21 
 22     /**
 23      * get Long from jsonObject
 24      * 
 25      * @param jsonObject
 26      * @param key
 27      * @param defaultValue
 28      * @return 
    29 *
  • if jsonObject is null, return defaultValue
  • 30 *
  • if key is null or empty, return defaultValue
  • 31 *
  • if {
  • @link JSONObject#getLong(String)} exception, return 32 * defaultValue 33 *
  • return {
  • @link JSONObject#getLong(String)} 34 * 35 */ 36 public static Long getLong(JSONObject jsonObject, String key, 37 Long defaultValue) { 38 if (jsonObject == null || StringUtil.isEmpty(key)) { 39 return defaultValue; 40 } 41 42 try { 43 return jsonObject.getLong(key); 44 } catch (JSONException e) { 45 if (isPrintException) { 46 e.printStackTrace(); 47 } 48 return defaultValue; 49 } 50 } 51 52 /** 53 * get Long from jsonData 54 * 55 * @param jsonData 56 * @param key 57 * @param defaultValue 58 * @return
      59 *
    • if jsonObject is null, return defaultValue
    • 60 *
    • if jsonData {
    • @link JSONObject#JSONObject(String)} exception, 61 * return defaultValue 62 *
    • return
    • 63 * {@link JSONUtils#getLong(JSONObject, String, JSONObject)} 64 * 65 */ 66 public static Long getLong(String jsonData, String key, Long defaultValue) { 67 if (StringUtil.isEmpty(jsonData)) { 68 return defaultValue; 69 } 70 71 try { 72 JSONObject jsonObject = new JSONObject(jsonData); 73 return getLong(jsonObject, key, defaultValue); 74 } catch (JSONException e) { 75 if (isPrintException) { 76 e.printStackTrace(); 77 } 78 return defaultValue; 79 } 80 } 81 82 /** 83 * @param jsonObject 84 * @param key 85 * @param defaultValue 86 * @return 87 * @see JSONUtils#getLong(JSONObject, String, Long) 88 */ 89 public static long getLong(JSONObject jsonObject, String key, 90 long defaultValue) { 91 return getLong(jsonObject, key, (Long) defaultValue); 92 } 93 94 /** 95 * @param jsonData 96 * @param key 97 * @param defaultValue 98 * @return 99 * @see JSONUtils#getLong(String, String, Long) 100 */ 101 public static long getLong(String jsonData, String key, long defaultValue) { 102 return getLong(jsonData, key, (Long) defaultValue); 103 } 104 105 /** 106 * get Int from jsonObject 107 * 108 * @param jsonObject 109 * @param key 110 * @param defaultValue 111 * @return
        112 *
      • if jsonObject is null, return defaultValue
      • 113 *
      • if key is null or empty, return defaultValue
      • 114 *
      • if {
      • @link JSONObject#getInt(String)} exception, return 115 * defaultValue 116 *
      • return {
      • @link JSONObject#getInt(String)} 117 * 118 */ 119 public static Integer getInt(JSONObject jsonObject, String key, 120 Integer defaultValue) { 121 if (jsonObject == null || StringUtil.isEmpty(key)) { 122 return defaultValue; 123 } 124 125 try { 126 return jsonObject.getInt(key); 127 } catch (JSONException e) { 128 if (isPrintException) { 129 e.printStackTrace(); 130 } 131 return defaultValue; 132 } 133 } 134 135 /** 136 * get Int from jsonData 137 * 138 * @param jsonData 139 * @param key 140 * @param defaultValue 141 * @return
          142 *
        • if jsonObject is null, return defaultValue
        • 143 *
        • if jsonData {
        • @link JSONObject#JSONObject(String)} exception, 144 * return defaultValue 145 *
        • return
        • 146 * {@link JSONUtils#getInt(JSONObject, String, JSONObject)} 147 * 148 */ 149 public static Integer getInt(String jsonData, String key, 150 Integer defaultValue) { 151 if (StringUtil.isEmpty(jsonData)) { 152 return defaultValue; 153 } 154 155 try { 156 JSONObject jsonObject = new JSONObject(jsonData); 157 return getInt(jsonObject, key, defaultValue); 158 } catch (JSONException e) { 159 if (isPrintException) { 160 e.printStackTrace(); 161 } 162 return defaultValue; 163 } 164 } 165 166 /** 167 * @param jsonObject 168 * @param key 169 * @param defaultValue 170 * @return 171 * @see JSONUtils#getInt(JSONObject, String, Integer) 172 */ 173 public static int getInt(JSONObject jsonObject, String key, int defaultValue) { 174 return getInt(jsonObject, key, (Integer) defaultValue); 175 } 176 177 /** 178 * @param jsonObject 179 * @param key 180 * @param defaultValue 181 * @return 182 * @see JSONUtils#getInt(String, String, Integer) 183 */ 184 public static int getInt(String jsonData, String key, int defaultValue) { 185 return getInt(jsonData, key, (Integer) defaultValue); 186 } 187 188 /** 189 * get Double from jsonObject 190 * 191 * @param jsonObject 192 * @param key 193 * @param defaultValue 194 * @return
            195 *
          • if jsonObject is null, return defaultValue
          • 196 *
          • if key is null or empty, return defaultValue
          • 197 *
          • if {
          • @link JSONObject#getDouble(String)} exception, return 198 * defaultValue 199 *
          • return {
          • @link JSONObject#getDouble(String)} 200 * 201 */ 202 public static Double getDouble(JSONObject jsonObject, String key, 203 Double defaultValue) { 204 if (jsonObject == null || StringUtil.isEmpty(key)) { 205 return defaultValue; 206 } 207 208 try { 209 return jsonObject.getDouble(key); 210 } catch (JSONException e) { 211 if (isPrintException) { 212 e.printStackTrace(); 213 } 214 return defaultValue; 215 } 216 } 217 218 /** 219 * get Double from jsonData 220 * 221 * @param jsonData 222 * @param key 223 * @param defaultValue 224 * @return
              225 *
            • if jsonObject is null, return defaultValue
            • 226 *
            • if jsonData {
            • @link JSONObject#JSONObject(String)} exception, 227 * return defaultValue 228 *
            • return
            • 229 * {@link JSONUtils#getDouble(JSONObject, String, JSONObject)} 230 * 231 */ 232 public static Double getDouble(String jsonData, String key, 233 Double defaultValue) { 234 if (StringUtil.isEmpty(jsonData)) { 235 return defaultValue; 236 } 237 238 try { 239 JSONObject jsonObject = new JSONObject(jsonData); 240 return getDouble(jsonObject, key, defaultValue); 241 } catch (JSONException e) { 242 if (isPrintException) { 243 e.printStackTrace(); 244 } 245 return defaultValue; 246 } 247 } 248 249 /** 250 * @param jsonObject 251 * @param key 252 * @param defaultValue 253 * @return 254 * @see JSONUtils#getDouble(JSONObject, String, Double) 255 */ 256 public static double getDouble(JSONObject jsonObject, String key, 257 double defaultValue) { 258 return getDouble(jsonObject, key, (Double) defaultValue); 259 } 260 261 /** 262 * @param jsonObject 263 * @param key 264 * @param defaultValue 265 * @return 266 * @see JSONUtils#getDouble(String, String, Double) 267 */ 268 public static double getDouble(String jsonData, String key, 269 double defaultValue) { 270 return getDouble(jsonData, key, (Double) defaultValue); 271 } 272 273 /** 274 * get String from jsonObject 275 * 276 * @param jsonObject 277 * @param key 278 * @param defaultValue 279 * @return
                280 *
              • if jsonObject is null, return defaultValue
              • 281 *
              • if key is null or empty, return defaultValue
              • 282 *
              • if {
              • @link JSONObject#getString(String)} exception, return 283 * defaultValue 284 *
              • return {
              • @link JSONObject#getString(String)} 285 * 286 */ 287 public static String getString(JSONObject jsonObject, String key, 288 String defaultValue) { 289 if (jsonObject == null || StringUtil.isEmpty(key)) { 290 return defaultValue; 291 } 292 293 //这里使用optString是为了防止后台抛出异常 294 return jsonObject.optString(key); 295 } 296 297 /** 298 * get String from jsonData 299 * 300 * @param jsonData 301 * @param key 302 * @param defaultValue 303 * @return
                  304 *
                • if jsonObject is null, return defaultValue
                • 305 *
                • if jsonData {
                • @link JSONObject#JSONObject(String)} exception, 306 * return defaultValue 307 *
                • return
                • 308 * {@link JSONUtils#getString(JSONObject, String, JSONObject)} 309 * 310 */ 311 public static String getString(String jsonData, String key, 312 String defaultValue) { 313 if (StringUtil.isEmpty(jsonData)) { 314 return defaultValue; 315 } 316 317 try { 318 JSONObject jsonObject = new JSONObject(jsonData); 319 return getString(jsonObject, key, defaultValue); 320 } catch (JSONException e) { 321 if (isPrintException) { 322 e.printStackTrace(); 323 } 324 return defaultValue; 325 } 326 } 327 328 /** 329 * get String array from jsonObject 330 * 331 * @param jsonObject 332 * @param key 333 * @param defaultValue 334 * @return
                    335 *
                  • if jsonObject is null, return defaultValue
                  • 336 *
                  • if key is null or empty, return defaultValue
                  • 337 *
                  • if {
                  • @link JSONObject#getJSONArray(String)} exception, return 338 * defaultValue 339 *
                  • if {
                  • @link JSONArray#getString(int)} exception, return 340 * defaultValue 341 *
                  • return string array
                  • 342 * 343 */ 344 public static String[] getStringArray(JSONObject jsonObject, String key, 345 String[] defaultValue) { 346 if (jsonObject == null || StringUtil.isEmpty(key)) { 347 return defaultValue; 348 } 349 350 try { 351 JSONArray statusArray = jsonObject.getJSONArray(key); 352 if (statusArray != null) { 353 String[] value = new String[statusArray.length()]; 354 for (int i = 0; i ) { 355 value[i] = statusArray.getString(i); 356 } 357 return value; 358 } 359 } catch (JSONException e) { 360 if (isPrintException) { 361 e.printStackTrace(); 362 } 363 return defaultValue; 364 } 365 return defaultValue; 366 } 367 368 /** 369 * get String array from jsonData 370 * 371 * @param jsonData 372 * @param key 373 * @param defaultValue 374 * @return
                      375 *
                    • if jsonObject is null, return defaultValue
                    • 376 *
                    • if jsonData {
                    • @link JSONObject#JSONObject(String)} exception, 377 * return defaultValue 378 *
                    • return
                    • 379 * {@link JSONUtils#getStringArray(JSONObject, String, JSONObject)} 380 * 381 */ 382 public static String[] getStringArray(String jsonData, String key, 383 String[] defaultValue) { 384 if (StringUtil.isEmpty(jsonData)) { 385 return defaultValue; 386 } 387 388 try { 389 JSONObject jsonObject = new JSONObject(jsonData); 390 return getStringArray(jsonObject, key, defaultValue); 391 } catch (JSONException e) { 392 if (isPrintException) { 393 e.printStackTrace(); 394 } 395 return defaultValue; 396 } 397 } 398 399 /** 400 * get JSONObject from jsonObject 401 * 402 * @param jsonObject 403 * 404 * @param key 405 * @param defaultValue 406 * @return
                        407 *
                      • if jsonObject is null, return defaultValue
                      • 408 *
                      • if key is null or empty, return defaultValue
                      • 409 *
                      • if {
                      • @link JSONObject#getJSONObject(String)} exception, return 410 * defaultValue 411 *
                      • return {
                      • @link JSONObject#getJSONObject(String)} 412 * 413 */ 414 public static JSONObject getJSONObject(JSONObject jsonObject, String key, 415 JSONObject defaultValue) { 416 if (jsonObject == null || StringUtil.isEmpty(key)) { 417 return defaultValue; 418 } 419 420 try { 421 return jsonObject.getJSONObject(key); 422 } catch (JSONException e) { 423 if (isPrintException) { 424 e.printStackTrace(); 425 } 426 return defaultValue; 427 } 428 } 429 430 /** 431 * get JSONObject from jsonData 432 * 433 * @param jsonData 434 * @param key 435 * @param defaultValue 436 * @return
                          437 *
                        • if jsonObject is null, return defaultValue
                        • 438 *
                        • if jsonData {
                        • @link JSONObject#JSONObject(String)} exception, 439 * return defaultValue 440 *
                        • return
                        • 441 * {@link JSONUtils#getJSONObject(JSONObject, String, JSONObject)} 442 * 443 */ 444 public static JSONObject getJSONObject(String jsonData, String key, 445 JSONObject defaultValue) { 446 if (StringUtil.isEmpty(jsonData)) { 447 return defaultValue; 448 } 449 450 try { 451 JSONObject jsonObject = new JSONObject(jsonData); 452 return getJSONObject(jsonObject, key, defaultValue); 453 } catch (JSONException e) { 454 if (isPrintException) { 455 e.printStackTrace(); 456 } 457 return defaultValue; 458 } 459 } 460 461 /** 462 * get JSONArray from jsonObject 463 * 464 * @param jsonObject 465 * @param key 466 * @param defaultValue 467 * @return
                            468 *
                          • if jsonObject is null, return defaultValue
                          • 469 *
                          • if key is null or empty, return defaultValue
                          • 470 *
                          • if {
                          • @link JSONObject#getJSONArray(String)} exception, return 471 * defaultValue 472 *
                          • return {
                          • @link JSONObject#getJSONArray(String)} 473 * 474 */ 475 public static JSONArray getJSONArray(JSONObject jsonObject, String key, 476 JSONArray defaultValue) { 477 if (jsonObject == null || StringUtil.isEmpty(key)) { 478 return defaultValue; 479 } 480 481 try { 482 return jsonObject.getJSONArray(key); 483 } catch (JSONException e) { 484 if (isPrintException) { 485 e.printStackTrace(); 486 } 487 return defaultValue; 488 } 489 } 490 491 /** 492 * get JSONArray from jsonData 493 * 494 * @param jsonData 495 * @param key 496 * @param defaultValue 497 * @return
                              498 *
                            • if jsonObject is null, return defaultValue
                            • 499 *
                            • if jsonData {
                            • @link JSONObject#JSONObject(String)} exception, 500 * return defaultValue 501 *
                            • return
                            • 502 * {@link JSONUtils#getJSONArray(JSONObject, String, JSONObject)} 503 * 504 */ 505 public static JSONArray getJSONArray(String jsonData, String key, 506 JSONArray defaultValue) { 507 if (StringUtil.isEmpty(jsonData)) { 508 return defaultValue; 509 } 510 511 try { 512 JSONObject jsonObject = new JSONObject(jsonData); 513 return getJSONArray(jsonObject, key, defaultValue); 514 } catch (JSONException e) { 515 if (isPrintException) { 516 e.printStackTrace(); 517 } 518 return defaultValue; 519 } 520 } 521 522 /** 523 * get Boolean from jsonObject 524 * 525 * @param jsonObject 526 * @param key 527 * @param defaultValue 528 * @return
                                529 *
                              • if jsonObject is null, return defaultValue
                              • 530 *
                              • if key is null or empty, return defaultValue
                              • 531 *
                              • return {
                              • @link JSONObject#getBoolean(String)} 532 * 533 */ 534 public static boolean getBoolean(JSONObject jsonObject, String key, 535 Boolean defaultValue) { 536 if (jsonObject == null || StringUtil.isEmpty(key)) { 537 return defaultValue; 538 } 539 540 try { 541 return jsonObject.getBoolean(key); 542 } catch (JSONException e) { 543 if (isPrintException) { 544 e.printStackTrace(); 545 } 546 return defaultValue; 547 } 548 } 549 550 /** 551 * get Boolean from jsonData 552 * 553 * @param jsonData 554 * @param key 555 * @param defaultValue 556 * @return
                                  557 *
                                • if jsonObject is null, return defaultValue
                                • 558 *
                                • if jsonData {
                                • @link JSONObject#JSONObject(String)} exception, 559 * return defaultValue 560 *
                                • return
                                • 561 * {@link JSONUtils#getBoolean(JSONObject, String, Boolean)} 562 * 563 */ 564 public static boolean getBoolean(String jsonData, String key, 565 Boolean defaultValue) { 566 if (StringUtil.isEmpty(jsonData)) { 567 return defaultValue; 568 } 569 570 try { 571 JSONObject jsonObject = new JSONObject(jsonData); 572 return getBoolean(jsonObject, key, defaultValue); 573 } catch (JSONException e) { 574 if (isPrintException) { 575 e.printStackTrace(); 576 } 577 return defaultValue; 578 } 579 } 580 581 /** 582 * get map from jsonObject. 583 * 584 * @param jsonObject 585 * key-value pairs json 586 * @param key 587 * @return
                                    588 *
                                  • if jsonObject is null, return null
                                  • 589 *
                                  • return {
                                  • @link JSONUtils#parseKeyAndValueToMap(String)} 590 * 591 */ 592 public static Map getMap(JSONObject jsonObject, String key) { 593 return JSONUtil.parseKeyAndValueToMap(JSONUtil.getString(jsonObject, 594 key, null)); 595 } 596 597 /** 598 * get map from jsonData. 599 * 600 * @param jsonData 601 * key-value pairs string 602 * @param key 603 * @return
                                      604 *
                                    • if jsonData is null, return null
                                    • 605 *
                                    • if jsonData length is 0, return empty map
                                    • 606 *
                                    • if jsonData {
                                    • @link JSONObject#JSONObject(String)} exception, 607 * return null 608 *
                                    • return {
                                    • @link JSONUtils#getMap(JSONObject, String)} 609 * 610 */ 611 public static Map getMap(String jsonData, String key) { 612 613 if (jsonData == null) { 614 return null; 615 } 616 if (jsonData.length() == 0) { 617 return new HashMap(); 618 } 619 620 try { 621 JSONObject jsonObject = new JSONObject(jsonData); 622 return getMap(jsonObject, key); 623 } catch (JSONEx


                                      评论


                                      亲,登录后才可以留言!