Log format output JSON content

Source: Internet
Author: User

One of the things that I used to do is to copy the JSON returned to the Format tool and look at the contents, and the JSON format method is not very convenient to format the log directly.

A simple Log class written earlier (Log.java)

Importandroid.text.TextUtils;/*** Log information * *@authorHarlan Song * @createDate 2013-2-8*/ Public classLog {/*** Log Switch*/    Private Static BooleanIsdebug =true; Private Static FinalString AUTHOR = "HARLAN--";  Public Static voidDebugBooleanstatus) {Isdebug=status; }     Public Static voidd (string tag, string message) {if(isdebug) {android.util.log.d (Tag,author+message); }    }     Public Static voidI (string tag, string message) {if(isdebug) {ANDROID.UTIL.LOG.I (tag, AUTHOR+message); }    }    /*** JSON formatted OUTPUT *@paramTag *@paramMessage content *@paramisoutputoriginalcontent whether to enter the original content*/     Public Static voidIjsonformat (string tag, string message,Booleanisoutputoriginalcontent) {        if(Isdebug &&!)textutils.isempty (message)) {            if(isoutputoriginalcontent) android.util.log.i (tag, AUTHOR+message); ANDROID.UTIL.LOG.I (tag, AUTHOR+ "\ n" +Jsonutils.format (stringutils.convertunicode (message))); }    }     Public Static voidW (string tag, string message) {if(isdebug) {ANDROID.UTIL.LOG.W (Tag,author+message); }    }     Public Static voide (String tag, string message) {if(isdebug) {ANDROID.UTIL.LOG.E (Tag,author+message); }    }}

Call Ijsonformat, first the content Unicode conversion and then format the JSON, or if the display is Unicode after the content of Chinese can not see it is not convenient.


Unicode decoding method, a lot of online.

  Public Staticstring Convertunicode (string ori) {CharAchar; intLen =ori.length (); StringBuffer Outbuffer=NewStringBuffer (len);  for(intx = 0; X <Len;) {Achar= Ori.charat (x + +); if(Achar = = ' \ \ ') {Achar= Ori.charat (x + +); if(Achar = = ' U ') {                    //Read the XXXX                    intValue = 0;  for(inti = 0; I < 4; i++) {Achar= Ori.charat (x + +); Switch(Achar) { Case' 0 ':                             Case' 1 ':                             Case' 2 ':                             Case' 3 ':                             Case' 4 ':                             Case' 5 ':                             Case' 6 ':                             Case' 7 ':                             Case' 8 ':                             Case' 9 ': Value= (value << 4) + Achar-' 0 ';  Break;  CaseA:                             Case' B ':                             CaseC:                             Case' d ':                             CaseE:                             Case' F ': Value= (value << 4) + + Achar-' a ';  Break;  CaseA:                             CaseB:                             CaseC:                             CaseD:                             CaseE:                             CaseF: Value= (value << 4) + + Achar-' A ';  Break; default:                                Throw NewIllegalArgumentException ("Malformed \\UXXXX encoding."); }} outbuffer.append ((Char) value); } Else {                    if(Achar = = ' t ') Achar= ' \ t '; Else if(Achar = = ' R ') Achar= ' \ r '; Else if(Achar = = ' n ') Achar= ' \ n '; Else if(Achar = = ' F ') Achar= ' \f ';                Outbuffer.append (Achar); }            } Elseoutbuffer.append (Achar); }        returnoutbuffer.tostring (); }


The JSON format method is also found online.

  Public Staticstring Format (string jsonstr) {intLevel = 0; StringBuffer Jsonformatstr=NewStringBuffer ();  for(intI=0;i<jsonstr.length (); i++){            Charc =Jsonstr.charat (i); if(level>0&& ' \ n ' ==jsonformatstr.charat (Jsonformatstr.length ()-1) {jsonformatstr.append (level)) {GETLEVELSTR); }            Switch(c) { Case‘{‘:                 Case‘[‘: Jsonformatstr.append (c+ "\ n"); level++;  Break;  Case‘,‘: Jsonformatstr.append (c+ "\ n");  Break;  Case‘}‘:                 Case‘]‘: Jsonformatstr.append ("\ n"); level--;                    Jsonformatstr.append (level) (GETLEVELSTR);                    Jsonformatstr.append (c);  Break; default: Jsonformatstr.append (c);  Break; }        }        returnjsonformatstr.tostring (); }    Private StaticString Getlevelstr (intLevel ) {StringBuffer Levelstr=NewStringBuffer ();  for(intLeveli = 0;leveli<level; leveli++) {levelstr.append ("\ T"); }        returnlevelstr.tostring (); }

END




Log format output JSON content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.