各种目标转成JSON字符串alibaba - 众发娱乐

各种目标转成JSON字符串alibaba

2019-02-13 11:06:18 | 作者: 运凯 | 标签: 东西,研讨,运转 | 浏览: 8029

今日研讨json,发现有个灰常牛X的东西,特别转载过来,以便今后学习。
需求用到的jar包
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.4.jar
json-lib-2.2.2-jdk15.jar
xom-1.0.jar

运转环境:
jdk1.6.0_23

下面就是代码了
这个JSONUtil东西类能够把任何类型的目标转换成JSON字符串

package com.json;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JSONUtil {
 private static Log log = LogFactory.getLog(JSONUtil.class);
 public static String object2json(Object obj) {
 StringBuilder json = new StringBuilder();
 if (obj  null) {
 json.append("\"\"");
 } else if (obj instanceof String || obj instanceof Integer
 || obj instanceof Float || obj instanceof Boolean
 || obj instanceof Short || obj instanceof Double
 || obj instanceof Long || obj instanceof BigDecimal
 || obj instanceof BigInteger || obj instanceof Byte) {
 json.append("\"").append(string2json(obj.toString())).append("\"");
 } else if (obj instanceof Object[]) {
 json.append(array2json((Object[]) obj));
 } else if (obj instanceof List) {
 json.append(list2json((List ? ) obj));
 } else if (obj instanceof Map) {
 json.append(map2json((Map ?, ? ) obj));
 } else if (obj instanceof Set) {
 json.append(set2json((Set ? ) obj));
 } else {
 json.append(bean2json(obj));
 return json.toString();
 public static String bean2json(Object bean) {
 StringBuilder json = new StringBuilder();
 json.append("{");
 PropertyDescriptor[] props = null;
 try {
 props = Introspector.getBeanInfo(bean.getClass(), Object.class)
 .getPropertyDescriptors();
 } catch (IntrospectionException e) {
 if (props != null) {
 for (int i = 0; i props.length; i++) {
 try {
 String name = object2json(props[i].getName());
 String value = object2json(props[i].getReadMethod().invoke(
 bean));
 json.append(name);
 json.append(":");
 json.append(value);
 json.append(",");
 } catch (Exception e) {
 json.setCharAt(json.length() - 1, });
 } else {
 json.append("}");
 return json.toString();
 public static String list2json(List ? list) {
 StringBuilder json = new StringBuilder();
 json.append("[");
 if (list != null list.size() 0) {
 for (Object obj : list) {
 json.append(object2json(obj));
 json.append(",");
 json.setCharAt(json.length() - 1, ]);
 } else {
 json.append("]");
 return json.toString();
 public static String array2json(Object[] array) {
 StringBuilder json = new StringBuilder();
 json.append("[");
 if (array != null array.length 0) {
 for (Object obj : array) {
 json.append(object2json(obj));
 json.append(",");
 json.setCharAt(json.length() - 1, ]);
 } else {
 json.append("]");
 return json.toString();
 public static String map2json(Map ?, ? map) {
 StringBuilder json = new StringBuilder();
 json.append("{");
 if (map != null map.size() 0) {
 for (Object key : map.keySet()) {
 json.append(object2json(key));
 json.append(":");
 json.append(object2json(map.get(key)));
 json.append(",");
 json.setCharAt(json.length() - 1, });
 } else {
 json.append("}");
 return json.toString();
 public static String set2json(Set ? set) {
 StringBuilder json = new StringBuilder();
 json.append("[");
 if (set != null set.size() 0) {
 for (Object obj : set) {
 json.append(object2json(obj));
 json.append(",");
 json.setCharAt(json.length() - 1, ]);
 } else {
 json.append("]");
 return json.toString();
 public static String string2json(String s) {
 if (s  null)
 return "";
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i s.length(); i++) {
 char ch = s.charAt(i);
 switch (ch) {
 case ":
 sb.append("\\\"");
 break;
 case \\:
 sb.append("\\\\");
 break;
 case \b:
 sb.append("\\b");
 break;
 case \f:
 sb.append("\\f");
 break;
 case \n:
 sb.append("\\n");
 break;
 case \r:
 sb.append("\\r");
 break;
 case \t:
 sb.append("\\t");
 break;
 case /:
 sb.append("\\/");
 break;
 default:
 if (ch = \u0000 ch = \u001F) {
 String ss = Integer.toHexString(ch);
 sb.append("\\u");
 for (int k = 0; k 4 - ss.length(); k++) {
 sb.append(0);
 sb.append(ss.toUpperCase());
 } else {
 sb.append(ch);
 return sb.toString();


最终上传自己写的小工程
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表众发娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章