jsonAdnPojo


@SuppressWarnings("unchecked")
public static String toJSONString(Object value) {
if (value == null) return "null";
if (value instanceof Number) return value.toString();
if (value instanceof Boolean) return value.toString();
if (value instanceof String)
return "\"" + escape((String) value) + "\"";
if (value instanceof Double) {
if (((Double) value).isInfinite() || ((Double) value).isNaN())
return "null";
else
return value.toString();
}
if (value instanceof Float) {
if (((Float) value).isInfinite() || ((Float) value).isNaN())
return "null";
else
return value.toString();
}
if (value instanceof Map) return map2Json((Map) value);
if (value instanceof Collection) return coll2Json((Collection) value);
if (value.getClass().isArray()) return array2Json(value);
return pojo2Json(value);
}

static String array2Json(Object array) {
if (null == array) return "null" ;
StringBuffer sb = new StringBuffer();
sb.append('[');
// 此处减1是为了下面的 逗号 追加
int len = Array.getLength(array) - 1;
if (len > -1){
int i ;
for (i = 0; i < len; i++) {
sb.append(toJSONString(Array.get(array, i))).append(", ");
}
sb.append(toJSONString(Array.get(array, i)));
}
sb.append(']');
return sb.toString();
}
static String coll2Json(Collection coll) {
if (null == coll) return "null" ;
StringBuffer sb = new StringBuffer();
sb.append('[');
for (Iterator it = coll.iterator(); it.hasNext();) {
sb.append(toJSONString(it.next()));
if (it.hasNext())
sb.append(", ");
}
sb.append(']');
return sb.toString();
};

你可能感兴趣的:(J2SE,F#)