/**
* HashMap转Json
*
* @param map 原map
* @return string类型的json
*/
public static String hashMapToJson(HashMap map) {
String string = "{";
for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry e = (Map.Entry) it.next();
string += "\"" + e.getKey() + "\":";
string += "\"" + e.getValue() + "\",";
}
string = string.substring(0, string.lastIndexOf(","));
string += "}";
return string;
}
-
-
-
public static String treeMapToJson(TreeMap map) {-
String string = "{";
for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry e = (Map.Entry) it.next();
string += "\"" + e.getKey() + "\":";
string += "\"" + e.getValue() + "\",";
}
string = string.substring(0, string.lastIndexOf(","));
string += "}";
return string;
}
-
-
/**-
* String转成Map
* Json 转成 Map<>
*
* @param jsonStr
* @return
*/
public static TreeMap<String, String> StringToMap(String jsonStr) {
TreeMap map = new TreeMap();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonStr);
Iterator<String> keyIter = jsonObject.keys();
String key;
Object value;
// Map<String, Object> valueMap = new HashMap<String, Object>();
while (keyIter.hasNext()) {
key = keyIter.next();
value = jsonObject.get(key);
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
Log_e("UtilsTools", "StringToMap --- ERROR --- " + e.getMessage());
}
return map;
}
-
-
/**-
* 把Map的key 转成 List
*
* @param map
* @return
*/
public static List<String> MapKeyToList(HashMap<String, String> map) {
List<String> firstList = new ArrayList<>();
HashMap<String, String> hashMap = map;
Iterator i = hashMap.entrySet().iterator();
while (i.hasNext()) {
Entry<String, String> entry = (Entry) i.next();
firstList.add(entry.getKey());
}
return firstList;
}
-
-
/**-
* JSONArray转换成List
*
* @param jsonArray
* @return
*/
public static List<String> JsonArrayToList(JSONArray jsonArray) {
List<String> firstList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
String s = jsonArray.get(i).toString();
firstList.add(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
return firstList;
}
-
-
/**
* 把Map的value 转成 List
*
* @param map
* @return
*/
public static List<String> MapValueToList(HashMap<String, String> map) {
List<String> firstList = new ArrayList<>();
HashMap<String, String> hashMap = map;
Iterator i = hashMap.entrySet().iterator();
while (i.hasNext()) {
Entry<String, String> entry = (Entry) i.next();
firstList.add(entry.getValue());
}
return firstList;
}
-
-
-
public static Map<String, String> JsonToMap(String data) {
Map<String, String> map = new HashMap<String, String>();
try {
JSONObject j = new JSONObject(data);
Iterator<String> it = j.keys();
while (it.hasNext()) {
String key = it.next().toString();
String value = j.getString(key);
map.put(key, value);
}
} catch (JSONException e) {
e.printStackTrace();
}
return map;
}