:
/ * * Créé le 4 novembre 2016 * * TODO pour modifier le modèle pour ce fichier généré Accédez à * Window - Préférences - Java - Style de code - Templates de code * / package com.suning.commerce.util; import java.sql.date; import java.sql.time; import java.sql.timestamp; import java.util.collection; java.util.Iterator;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;/** * @author nicholas tse * * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates */public class JsonUtils { /** * * @param array * @return */ private String static array2json (objet [] array) {if (array.length == 0) return "[]"; int i = array.length; StringBuffer sb = new StringBuffer (i << 4); SB.APPEND ('['); pour (int j = 0; j <i; j ++) {objet o = array [j]; SB.APPEND (TOJSON (O)); SB.APPEND (','); } // 将最后添加的 ',' 变为 ']': SB.Setcharat (sb.length () - 1, ']'); return sb.toString (); } String statique privé String 2json (String S) {StringBuffer SB = new StringBuffer (S.LengthD () + 20); sb.append ('/ "'); for (int i = 0; i <s.Length (); i ++) {char c = s.charat (i); switch (c) {case '/"': sb.append ("///" "); cass; '/ b': SB.APPELLAGE (// b "); } } sb.append ('/ "'); return sb.toString (); } String statique public tojson (objet o) {if (o == null) {return "null"; } else if (o instanceof string) {return string2json ((string) o); } else if ((o instanceof boolean) || (o instanceof numéro)) {return o.toString (); } else if ((o instanceof date) || (o instanceof time) || o instanceof timestamp) {return o.toString (); } else if (o instanceof java.util.date) {return dateUtil.formatdate ((java.util.date) o, "yyyy-mm-dd hh: mm: ss"); } else if (o instanceof map) {return map2json ((map) o); } else if (o instanceOf object []) {return array2json ((objet []) o); } else if (o instanceOf Collection) {return array2json ((collection) o) .toArray ()); } else {try {map décrire = beanutils.describe (o); RETOUR MAP2JSON (décrire); } catch (exception e) {// TODO Bloc de capture généré automatiquement e.printStackTrace (); }} Throw New RuntimeException ("Type non soutenu:" + o.getClass (). getName ()); } / ** * * @param map * @return * / private static String map2json (map map) {if (map.isempty ()) return "{}"; StringBuffer sb = new StringBuffer (map.size () << 4); SB.APPEND ('{'); Iterator iterator = map.KeySet (). Iterator (); while (iterator.hasnext ()) {objet key = iterator.next (); Valeur d'objet = map.get (key); sb.append ('/ "'); sb.append (key); sb.append ('/"'); SB.APPEND (':'); SB.APPEND (TOJSON (valeur)); SB.APPEND (','); } // 将最后的 ',' 变为 '}': SB.Setcharat (sb.length () - 1, '}'); return sb.toString (); }}以上就是小编为大家带来的 JSON 自定义使用函数的简单实例全部内容了 , 希望大家多多支持武林网 ~