Convert object object to JSON string

Source: Internet
Author: User
Tags getmessage

    /** * Divide the object into JSON string (JSON + recursive) * @param the obj parameter should be {@link Java.util.Map} or {@link java.util.List} * @return */@SuppressWarnings ("unchecked")     Public Staticobject Jsonenclose (Object obj) {Try {            if(obj instanceof Map) {//if it's a map, convert it to Jsonobject .map<string, object> map = (map<string, object>) obj; Iterator<entry<string, object>> iterator =Map.entryset (). iterator (); Jsonstringer Jsonstringer=NewJsonstringer ().Object();  while(Iterator.hasnext ()) {Entry<string, object> entry =Iterator.next ();                Jsonstringer.key (Entry.getkey ()). Value (Jsonenclose (Entry.getvalue ())); } jsonobject Jsonobject=NewJsonobject (NewJsontokener (Jsonstringer.endobject (). toString ())); returnJsonobject; } Else if(obj instanceof List) {//if the list is converted to Jsonarraylist<object> list = (list<object>) obj; Jsonstringer Jsonstringer=NewJsonstringer (). Array ();  for(inti =0; I < list.size (); i++) {jsonstringer.value (Jsonenclose (list.Get(i))); } Jsonarray Jsonarray=NewJsonarray (NewJsontokener (Jsonstringer.endarray (). toString ())); returnJsonarray; } Else {                returnobj; }        } Catch(Exception e) {LOG.E ("Jsonutil--enclose", E.getmessage ()); returnE.getmessage (); }    }

Convert object object to JSON string

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.