常用类型转换(随笔)

Map转Json

import com.alibaba.fastjson.JSONObject;

JSONObject json = new JSONObject(map);

JSON转String

import com.alibaba.fastjson.JSONObject;

json.toJSONString()

String转Map(String为map.toString()格式)

public static Map mapString2Map(String str) {
		str = str.substring(1, str.length() - 1);
		String[] strs = str.split(",");
		Map map = new HashMap();
		for (String string : strs) {
			String key = string.split("=")[0];
			String value = string.split("=")[1];
			String key1 = key.trim();
			String value1 = value.trim();
			map.put(key1, value1);
		}
		return map;
	}

循环遍历Map

Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
	String key = iter.next();
	String val = map.get(key);
}

大写字母转小写字母

public static String toLowerCase(String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); ++i) {
        char ch = s.charAt(i);
        if (ch >= 65 && ch <= 90) {
            ch |= 32;
        }
        sb.append(ch);
    }
    return sb.toString();
}

使用toLowerCase()和toUpperCase()进行字符串大小写转换

String str = "Abc";
//大写转小写
str.toLowerCase(); //abc

//小写转大写
str.toUpperCase();//ABC

List < String > 转String[]

String[] str = list.toArray(new String[0]);

  原作:

常用类型转换(随笔)_白snow的博客-CSDN博客

你可能感兴趣的:(java-转换,java,json)