//
public static HashMap<String, Object> parseJsonFinal(String json) {
HashMap<String, Object> map = new HashMap<String, Object>();try {
JSONObject object = new JSONObject(json);
@SuppressWarnings("unchecked")
Iterator<String> iterator = object.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object obj = object.get(key);
if (obj instanceof String || obj instanceof Integer
|| obj instanceof Boolean || obj instanceof Double) {
String value = obj.toString();
map.put(key, value);
} else if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) obj;
ArrayList<HashMap<String, Object>> mapList = new ArrayList<HashMap<String, Object>>();
int m = jsonArray.length();
if (m > 0) {
for (int j = 0; j < m; j++) {
JSONObject o = jsonArray.getJSONObject(j);
mapList.add(getMapFromJsonObject(o));
}
}
map.put(key, mapList);
} else if (obj instanceof JSONObject) {
// mapList.add(getMapFromJsonObject(o));
map.put(key, getMapFromJsonObject((JSONObject) obj));
} else if (obj instanceof Object[]) {
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
//解析jsonObject
public static HashMap<String, Object> getMapFromJsonObject(JSONObject object) {
HashMap<String, Object> map = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
Iterator<String> iterator = object.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
try {
Object obj = object.get(key);
if ((obj instanceof String) || obj instanceof Double
|| obj instanceof Float || (obj instanceof Integer)
|| (obj instanceof Boolean)) {
String value = obj.toString();
map.put(key, value);
} else if (obj instanceof JSONObject) {
// mapList.add(getMapFromJsonObject(o));
map.put(key, getMapFromJsonObject((JSONObject) obj));
} else if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) obj;
ArrayList<String> mapList = new ArrayList<String>();
ArrayList<HashMap<String, Object>> mapList1 = new ArrayList<HashMap<String, Object>>();
boolean isobj = true;
int m = jsonArray.length();
if (m > 0) {
for (int j = 0; j < m; j++) {
// JSONObject o = jsonArray.getJSONObject(j);
if (jsonArray.get(j) instanceof JSONObject) {
isobj = false;
mapList1.add(getMapFromJsonObject(jsonArray
.getJSONObject(j)));
} else {
mapList.add(jsonArray.get(j) + "");
isobj = true;
}
}
}
if (isobj)
map.put(key, mapList);
else
map.put(key, mapList1);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return map;
}