Json 自定义使用函数的简单实例

时间:2022-05-19 22:27:21

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Created on Nov 4, 2016
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.suning.commerce.util;
 
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collection;
import 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 static String array2Json(Object[] array) {
    if (array.length == 0)
      return "[]";
    int i = array.length;
    StringBuffer sb = new StringBuffer(i << 4);
    sb.append('[');
    for (int j = 0; j < i; j++) {
      Object o = array[j];
      sb.append(toJson(o));
      sb.append(',');
    }
    // 将最后添加的 ',' 变为 ']':
    sb.setCharAt(sb.length() - 1, ']');
    return sb.toString();
  }
 
  private static String string2Json(String s) {
    StringBuffer sb = new StringBuffer(s.length() + 20);
    sb.append('\"');
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      switch (c) {
      case '\"':
        sb.append("\\\"");
        break;
      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;
      default:
        sb.append(c);
      }
    }
    sb.append('\"');
    return sb.toString();
  }
 
  public static String toJson(Object o) {
    if (o == null) {
      return "null";
    } else if (o instanceof String) {
      return string2Json((String) o);
    } else if ((o instanceof Boolean) || (o instanceof Number)) {
      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((Object[]) o);
    } else if (o instanceof Collection) {
      return array2Json(((Collection) o).toArray());
    } else {
      try {
        Map describe = BeanUtils.describe(o);
        return map2Json(describe);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    throw new RuntimeException("Unsupported type: " + 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()) {
      Object key = iterator.next();
      Object value = map.get(key);
      sb.append('\"');
      sb.append(key);
      sb.append('\"');
      sb.append(':');
      sb.append(toJson(value));
      sb.append(',');
    }
    // 将最后的 ',' 变为 '}':
    sb.setCharAt(sb.length() - 1, '}');
    return sb.toString();
  }
}

以上就是小编为大家带来的Json 自定义使用函数的简单实例全部内容了,希望大家多多支持服务器之家~