/** * 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