==
去年写的两个小类,代码很简单。
==
==
toJson
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Jsonhelp {
public static void main(String[] args) {
HashMap<Object, Object> map = new HashMap<Object, Object>();
map.put("112", "12");
map.put("1ui", 3);
Y t = new Y();
map.put("c", t);
ArrayList<Y> list = new ArrayList<Y>();
list.add(t);
list.add(t);
Y[] t1 = {new Y(), new Y()};
map.put("list", list);
map.put("arr", t1);
System.out.println(toJson(map));
}
public static String toJson(Object o) {
try {
return toJsonMain(o);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Object fromJson(String o, Class c) {
// return new JsonParser(o,c).parse();
return null;
}
private static String toJsonMain(Object o) {
if (isBaseClass(o))
return toJsonBase(o);
if (isBase(o)) {
if (o.getClass() == String.class)
return "\"" + o + "\"";
return o + "";
}
if (isArray(o)) {
return toJsonArray(o);
}
if (isCollection(o)) {
return toJsonCollection(o);
}
if (isMap(o)) {
return toJsonMap(o);
}
return "";
}
private static String toJsonBase(Object o) {
Class c = o.getClass();
Field[] fs = c.getDeclaredFields();
String result = "{";
if (fs == null || fs.length == 0) {
return "";
}
for (Field f : fs) {
f.setAccessible(true);
try {
Object value = f.get(o);
String key = f.getName();
if (value == null) {
value = "\"\"";
} else if (isBase(value)) {
value = "\"" + value + "\"";
} else {
value = toJsonMain(value);
}
result += "\"" + key + "\":" + value + ",";
} catch (Exception e) {
e.printStackTrace();
}
}
return result.substring(0, result.length() - 1) + "}";
}
private static String toJsonArray(Object o) {
if (o.getClass() == int[].class) {
return tointArrayJson(o);
}
if (o.getClass() == double[].class) {
return todoubleArrayJson(o);
}
if (o.getClass() == long[].class) {
return tolongArrayJson(o);
}
if (o.getClass() == char[].class) {
return tocharArrayJson(o);
}
if (o.getClass() == byte[].class) {
return tobyteArrayJson(o);
}
if (o.getClass() == boolean[].class) {
return tobooleanArrayJson(o);
}
if (o.getClass() == float[].class) {
return tofloatArrayJson(o);
}
if (o.getClass() == short[].class) {
return toshortArrayJson(o);
}
Object[] arr = (Object[]) o;
String result = "[";
for (Object a : arr) {
if (a == null) {
result += ",";
continue;
}
if (isBase(a)) {
result += (a + ",");
continue;
} else {
result += toJsonMain(a) + ",";
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String toJsonMap(Object o) {
Map<Object, Object> map = (Map<Object, Object>) o;
String result = "{";
Set<Object> set = map.keySet();
if (set.size() == 0)
return "{}";
Iterator<Object> iter = set.iterator();
while (iter.hasNext()) {
String str = iter.next() + "";
result += ("\"" + str + "\"");
Object obj = map.get(str);
if (isBase(obj)) {
if (obj.getClass() == String.class) {
result += ":" + "\"" + obj + "\",";
} else {
result += ":" + obj + ",";
}
} else {
result += (":" + toJsonMain(obj) + ",");
}
}
return result.substring(0, result.length() - 1) + "}";
}
private static String toJsonCollection(Object o) {
Collection<Object> cs = (Collection) o;
if (cs == null || cs.size() == 0) {
return "\"\"";
}
String resule = "[";
for (Object oc : cs) {
resule += toJsonMain(oc) + ",";
}
return resule.substring(0, resule.length() - 1) + "]";
}
private static boolean isArray(Object o) {
return o.getClass().toString().startsWith("class [");
}
private static boolean isMap(Object o) {
return isMapDo(o.getClass());
}
private static boolean isBaseClass(Object o) {
return !(isBase(o) || isArray(o) || isCollection(o) || isMap(o));
}
private static boolean isCollection(Object o) {
return isCollectionDo(o.getClass());
}
private static boolean isMapDo(Class o) {
Class[] interfaces = o.getInterfaces();
for (Class i : interfaces) {
if (i == Map.class)
return true;
if (i.getInterfaces() != null)
return isMapDo(i);
}
return false;
}
private static boolean isCollectionDo(Class o) {
Class[] interfaces = o.getInterfaces();
for (Class i : interfaces) {
if (i == Collection.class)
return true;
if (i.getInterfaces() != null)
return isCollectionDo(i);
}
return false;
}
private static boolean isBase(Object o) {
Class c = o.getClass();
if (c == Integer.class || c == Byte.class || c == Boolean.class
|| c == Double.class || c == Float.class || c == Long.class
|| c == String.class || c == Short.class
|| c == Character.class)
return true;
else
return false;
}
private static String tointArrayJson(Object o) {
int[] arr = (int[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String tolongArrayJson(Object o) {
long[] arr = (long[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String todoubleArrayJson(Object o) {
double[] arr = (double[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String tobooleanArrayJson(Object o) {
boolean[] arr = (boolean[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String tocharArrayJson(Object o) {
char[] arr = (char[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String tobyteArrayJson(Object o) {
byte[] arr = (byte[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String tofloatArrayJson(Object o) {
float[] arr = (float[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
private static String toshortArrayJson(Object o) {
short[] arr = (short[]) o;
String result = "[";
for (Object a : arr) {
if (isBase(a)) {
result += (a + ",");
}
}
return result.substring(0, result.length() - 1) + "]";
}
}
// some Class for Test
class P {
// int age = 12;
// int name = 11;
// int[] p = { 12, 4, 23 };
// Z z = new Z();
// Y y = new Y();
// Y[] y2 = { y, y };
// ArrayList<Y> c = new ArrayList<Y>();
HashMap<String, Object> map = new HashMap<String, Object>();
private void addList() {
// c.add(new Y());
map.put("123", "123");
map.put("ccc", new Y());
}
}
class Z {
int z = 1;
int zu = 12;
Y y = new Y();
}
class Y {
int hh = 90;
int xx = 99;
}
==
fromJson,这个代码找到了递归点后就上班了,一直也没有完成。。(⊙﹏⊙)b。。。
==
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonParser {
public String mainStr;
public Class c;
int flag = 0;
int flagtype = 0;
public Object obj;
public JsonParser(String o, Class c) {
this.mainStr = o;
this.c = c;
}
public static void main(String[] args) {
// \"name\":888,
String o = "{\"age\":123,\"nam1e\":{\"a2ge\":123}}";
String json = "{\"root\":" + o + "}";
System.out.println(json);
JsonParser p = new JsonParser(json, H.class);
// Map<String, Object> map = new HashMap<String, Object>();
// p.mainparse(map, "root");
Node objmap = (Node) p.parse();
H h = (H) p.getObject((Map) objmap.value);
System.out.println(h.age);
}
private Object getObject(Map objmap) {
try {
Class hclass = c;
Object h = hclass.newInstance();
Field[] fs = hclass.getDeclaredFields();
for (Field f : fs) {
String sva = (String) objmap.get(f.getName());
if (sva == null) {
continue;
}
if (f.getType() == int.class) {
f.set(h, Integer.parseInt(sva));
} else {
f.set(h, sva);
}
}
return h;
} catch (Exception e) {
e.printStackTrace();
return objmap;
}
}
int num1 = 0;// [的数量
int num2 = 0;// {的数量
char bc = ' ';//
private Object parse() {
// char c = mainStr.charAt(flag++);
flag++;
char c = getType();
// c = getType();
if (c == '{') {
return parseObj();
} else if (c == '[') {
return parseArr();
} else {
return null;
}
}
private List parseArr() {// 未实现功能
// String key = getKey();
// HashMap<Object, Object> mapp = new HashMap<Object, Object>();
List list = new ArrayList<Map>();
while (mainStr.charAt(flag++) == ',') {
char c = getType();
if (c == '{') {
list.add(parseObj());
} else if (c == '[') {
list.add(parseArr());
} else if (c == '"') {
list.add(parseString());
} else {
}
}
// mapp.put(key, list);
return list;
}
// root: {"arr":[{"hh":"90","xx":"99"},{"hh":"90","xx":"99"}],
private Node parseObj() {// { }
// flag++;
String key = getKey();
HashMap<Object, Object> mapp = new HashMap<Object, Object>();
Node node = new Node();
node.key = key;
char c = getType();
if (c == '{') {
Node n = parseObj();
mapp.put(n.key, n.value);
} else if (c == '[') {
// list.add(parseArr());
} else if (c == '"') {
// list.add(parseString());
Node n = parseString();
mapp.put(n.key, n.value);
} else {
}
while (mainStr.charAt(flag++) == ',') {
char d = getType();
if (d == '{') {
Node n = parseObj();
mapp.put(n.key, n.value);
} else if (d == '[') {
// list.add(parseArr());
} else if (d == '"') {
Node n = parseString();
mapp.put(n.key, n.value);
// list.add(parseString());
} else {
}
}
flag++;
node.value = mapp;
return node;
}
private char getType() {
while (mainStr.charAt(flagtype++) != ':') {
// break;
}
char c = mainStr.charAt(flagtype++);
if (c != '{' && c != '[' && c != '"') {
c = '"';
}
return c;// mainStr.charAt(flagtype++);
}
private Node parseString() {
// HashMap<Object, Object> m = new HashMap<Object, Object>();
String key = getKey();
String value = getValue();
// m.put(key, value);
Node n = new Node();
n.key = key;
n.value = value;
return n;
}
private Object getObj() {
try {
Object obj = c.newInstance();
Field[] fs = c.getDeclaredFields();
for (Field f : fs) {
f.setAccessible(true);
Class cc = f.getType();
if (cc == int.class) {
// if (map.containsKey(f.getName()))
// f.set(obj, Integer.parseInt("" + map.get(f.getName())));
}
}
return obj;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return null;
}
private String getKey() {
String s = "";
for (;;) {
char q = mainStr.charAt(flag - 1);
char c = mainStr.charAt(flag++);
if (c == '\"' && (q == '{' || q == '[' || q == ','))
break;
}
for (;;) {
char word = mainStr.charAt(flag++);
if (word == '\"' || word == ',') {
break;
}
s += word;
}
System.out.println("key是: " + s);
return s;
}
private String getValue() {
String s = "";
// 出问题
while (mainStr.charAt(flag++) == ':') {
if (mainStr.charAt(flag + 1) == '\"')
flag++;
break;
}
for (;;) {
char word = mainStr.charAt(flag++);
if (word == '\"' || word == ',' || word == '}') {
flag--;
break;
}
s += word;
}
System.out.println("value是: " + s);
return s;
}
class Node {
String key;
Object value;
}
}
class H {
int age = 123;
int name = 888;
}
==