MCPcopy Create free account
hub / github.com/afkT/DevUtils / jsonTag

Method jsonTag

lib/DevApp/src/main/java/dev/utils/LogPrintUtils.java:394–438  ·  view source on GitHub ↗

将 JSON 缩进格式化后打印为 DEBUG 日志 内容为空时打印 ERROR 提示;解析失败时打印异常与原始 JSON。 @param tag 日志 TAG @param json JSON 字符串

(
            final String tag,
            final String json
    )

Source from the content-addressed store, hash-verified

392 * @param json JSON 字符串
393 */
394 public static void jsonTag(
395 final String tag,
396 final String json
397 ) {
398 if (JUDGE_PRINT_LOG) {
399 // 判断传入 JSON 格式信息是否为 null
400 if (isEmpty(json)) {
401 printLog(Log.ERROR, tag, "Empty/Null json content");
402 return;
403 }
404 try {
405 // 属于对象的 JSON 格式信息
406 if (json.startsWith("{")) {
407 JSONObject jsonObject = new JSONObject(json);
408 // 进行缩进
409 String message = jsonObject.toString(JSON_INDENT);
410 // 打印信息
411 printLog(Log.DEBUG, tag, message);
412 } else if (json.startsWith("[")) {
413 // 属于数据的 JSON 格式信息
414 JSONArray jsonArray = new JSONArray(json);
415 // 进行缩进
416 String message = jsonArray.toString(JSON_INDENT);
417 // 打印信息
418 printLog(Log.DEBUG, tag, message);
419 } else {
420 // 打印信息
421 printLog(Log.DEBUG, tag, "json content format error");
422 }
423 } catch (Exception e) {
424 String errorInfo;
425 Throwable throwable = e.getCause();
426 if (throwable != null) {
427 errorInfo = throwable.toString();
428 } else {
429 try {
430 errorInfo = e.toString();
431 } catch (Exception e1) {
432 errorInfo = e1.toString();
433 }
434 }
435 printLog(Log.ERROR, tag, errorInfo + DevFinal.SYMBOL.NEW_LINE + json);
436 }
437 }
438 }
439
440 /**
441 * 将 XML 格式化缩进后打印为 DEBUG 日志

Callers 1

jsonMethod · 0.95

Calls 3

isEmptyMethod · 0.95
printLogMethod · 0.95
toStringMethod · 0.45

Tested by

no test coverage detected