android - format json string in adb logcat output

Michael picture Michael · Apr 7, 2013 · Viewed 29.6k times · Source

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?

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

Answer

Deepak Bala picture Deepak Bala · Apr 7, 2013

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

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

Output

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