2013-04-07 3 views
19

Pour la consignation, j'imprime la chaîne de réponse json et je peux les voir dans la commande android "adb logcat". Existe-t-il un moyen de formater correctement la chaîne json dans la sortie adb logcat afin qu'elle ressemble à ceci?Chaîne json au format android dans la sortie adb logcat

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

Répondre

44

Vous pouvez utiliser la méthode JSONObject.toString() pour enluminer le JSON sur logcat.

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

Sortie

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

génial, merci! – Michael

+3

@DeepakBala Qu'est-ce que '4' dans' Log.d ("tag", jsonObject.toString (4)); '? Est-ce l'espacement? –

+2

Oui, c'est un [indent factor] (http://www.json.org/javadoc/org/json/JSONObject.html#toString (int)) –

6

Je n'imprime pas assez le message JSON dans le code. J'ai juste double-cliqué sur le message en LogRabbit sur Mac et il va plutôt l'imprimer ou surligner Base64 pour le décoder.

enter image description here

divulgation complète, je suis le créateur de LogRabbit pour Mac.

+0

J'ai ajouté un tutoriel vidéo pour le filtrage de tous les messages qui contiennent JSON dans vos fichiers logcat ici https://www.youtube.com/regarder? v = HeildKynnuc – Yepher

2

Vous pouvez JSON dans le journal avec la forme originale par code d'utilisation de mon enregistreur:

Lien sur 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; 
    } 
} 
1

fastJson fournissent une méthode:

//serialize a prettyFormat json string 

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

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

 

Questions connexes