在adb logcat输出中格式化json字符串

时间:2020-12-31 14:00:54

For logging purpose, I print out json response string and can see them in android "adb logcat" command. Is there a way to nicely format the json string in adb logcat output so that it looks like this?

为了进行日志记录,我打印出json响应字符串,并可以在android“adb logcat”命令中看到它们。是否有办法在adb logcat输出中很好地格式化json字符串,使其看起来像这样?

{ "code" : "0",
  "text" : "hello world"
} 

4 个解决方案

#1


47  

You can use the JSONObject.toString() method to pretty print the JSON on logcat.

可以使用JSONObject.toString()方法在logcat上漂亮地打印JSON。

Log.d("tag", jsonObject.toString(4));

Output

输出

(29124): {
(29124):     "text": "hello world",
(29124):     "code": "0"
(29124): }

#2


6  

I don't pretty print the JSON message in code. I just double click on the message in LogRabbit on Mac and it will pretty print it or highlight Base64 to decode it.

我不太喜欢用代码打印JSON消息。我只要双击Mac上的LogRabbit上的消息,它就会很漂亮地打印出来,或者高亮Base64进行解码。

在adb logcat输出中格式化json字符串

Full disclosure, I am the creator of LogRabbit for Mac.

坦白说,我是Mac的LogRabbit的创造者。

#3


2  

You can format json in log with original form by use code of my logger:

您可以使用我的日志记录器的代码将json格式化为原始形式的日志:

Logger.dd("Section label", json);

Link on Github: https://github.com/scijoker/logger

Github上链接:https://github.com/scijoker/logger

Source:

来源:

import android.os.Build;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by scijoker on 08.10.15.
 */
public class Logger {
    private static boolean DEBUG = true;

    public static void d(String tag, String arg) {
        if (isEnable()) {
            log(tag, arg);
        }
    }

    public static void d(String logMsg) {
        if (isEnable()) {
            log(getCurrentClassName(), getCurrentMethodName() + "(): " + logMsg);
        }
    }

    public static void dd(String tag, Object source) {
        if (isEnable()) {
            Object o = getJsonObjFromStr(source);
            if (o != null) {
                try {
                    if (o instanceof JSONObject) {
                        format(tag, ((JSONObject) o).toString(2));
                    } else if (o instanceof JSONArray) {
                        format(tag, ((JSONArray) o).toString(2));
                    } else {
                        format(tag, source);
                    }
                } catch (JSONException e) {
                    format(tag, source);
                }
            } else {
                format(tag, source);
            }
        }
    }

    private static void log(String tag, String msg) {
        Log.d(tag, msg);
    }

    private static String getSplitter(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append("-");
        }
        return builder.toString();
    }

    private static void format(String tag, Object source) {
        tag = " " + tag + " ";
        log(" ", " ");
        log(" ", getSplitter(50) + tag + getSplitter(50));
        log(" ", "" + source);
        log(" ", getSplitter(100 + tag.length()));
        log(" ", " ");
    }

    private static String getCurrentMethodName() {
        return Thread.currentThread().getStackTrace()[4].getMethodName();
    }

    private static String getCurrentClassName() {
        String className = Thread.currentThread().getStackTrace()[4].getClassName();
        String[] temp = className.split("[\\.]");
        className = temp[temp.length - 1];
        return className;
    }

    private static Object getJsonObjFromStr(Object test) {
        Object o = null;
        try {
            o = new JSONObject(test.toString());
        } catch (JSONException ex) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    o = new JSONArray(test);
                }
            } catch (JSONException ex1) {
                return null;
            }
        }
        return o;
    }

    public static boolean isEnable() {
        return DEBUG;
    }

    public static void setEnable(boolean flag) {
        Logger.DEBUG = flag;
    }
}

#4


1  

fastJson provide a method:

fastJson提供一个方法:

//serialize a prettyFormat json string 

public static final String toJSONString(Object object, boolean prettyFormat);

eg:Log.d(TAG,JSON.toJSONString(object, true));

 

 

#1


47  

You can use the JSONObject.toString() method to pretty print the JSON on logcat.

可以使用JSONObject.toString()方法在logcat上漂亮地打印JSON。

Log.d("tag", jsonObject.toString(4));

Output

输出

(29124): {
(29124):     "text": "hello world",
(29124):     "code": "0"
(29124): }

#2


6  

I don't pretty print the JSON message in code. I just double click on the message in LogRabbit on Mac and it will pretty print it or highlight Base64 to decode it.

我不太喜欢用代码打印JSON消息。我只要双击Mac上的LogRabbit上的消息,它就会很漂亮地打印出来,或者高亮Base64进行解码。

在adb logcat输出中格式化json字符串

Full disclosure, I am the creator of LogRabbit for Mac.

坦白说,我是Mac的LogRabbit的创造者。

#3


2  

You can format json in log with original form by use code of my logger:

您可以使用我的日志记录器的代码将json格式化为原始形式的日志:

Logger.dd("Section label", json);

Link on Github: https://github.com/scijoker/logger

Github上链接:https://github.com/scijoker/logger

Source:

来源:

import android.os.Build;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by scijoker on 08.10.15.
 */
public class Logger {
    private static boolean DEBUG = true;

    public static void d(String tag, String arg) {
        if (isEnable()) {
            log(tag, arg);
        }
    }

    public static void d(String logMsg) {
        if (isEnable()) {
            log(getCurrentClassName(), getCurrentMethodName() + "(): " + logMsg);
        }
    }

    public static void dd(String tag, Object source) {
        if (isEnable()) {
            Object o = getJsonObjFromStr(source);
            if (o != null) {
                try {
                    if (o instanceof JSONObject) {
                        format(tag, ((JSONObject) o).toString(2));
                    } else if (o instanceof JSONArray) {
                        format(tag, ((JSONArray) o).toString(2));
                    } else {
                        format(tag, source);
                    }
                } catch (JSONException e) {
                    format(tag, source);
                }
            } else {
                format(tag, source);
            }
        }
    }

    private static void log(String tag, String msg) {
        Log.d(tag, msg);
    }

    private static String getSplitter(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append("-");
        }
        return builder.toString();
    }

    private static void format(String tag, Object source) {
        tag = " " + tag + " ";
        log(" ", " ");
        log(" ", getSplitter(50) + tag + getSplitter(50));
        log(" ", "" + source);
        log(" ", getSplitter(100 + tag.length()));
        log(" ", " ");
    }

    private static String getCurrentMethodName() {
        return Thread.currentThread().getStackTrace()[4].getMethodName();
    }

    private static String getCurrentClassName() {
        String className = Thread.currentThread().getStackTrace()[4].getClassName();
        String[] temp = className.split("[\\.]");
        className = temp[temp.length - 1];
        return className;
    }

    private static Object getJsonObjFromStr(Object test) {
        Object o = null;
        try {
            o = new JSONObject(test.toString());
        } catch (JSONException ex) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    o = new JSONArray(test);
                }
            } catch (JSONException ex1) {
                return null;
            }
        }
        return o;
    }

    public static boolean isEnable() {
        return DEBUG;
    }

    public static void setEnable(boolean flag) {
        Logger.DEBUG = flag;
    }
}

#4


1  

fastJson provide a method:

fastJson提供一个方法:

//serialize a prettyFormat json string 

public static final String toJSONString(Object object, boolean prettyFormat);

eg:Log.d(TAG,JSON.toJSONString(object, true));