1. 程式人生 > >Map<String,String>轉Json轉Base64

Map<String,String>轉Json轉Base64

exce configMap 數組 fas ont [] aci fast json

Map<String,String> configMap = new HashMap<String,String>();

System.out.println("JSON.toJSONString(configMap) is " + JSON.toJSONString(configMap)); //第一種 import com.alibaba.fastjson.JSON;
System.out.println("JsonUtils.toJson(configMap) is " + JsonUtils.toJson(configMap)); //第二種 import com.pisgah.common.util.JsonUtils;
try{
byte byteXl[] = JSON.toJSONString(configMap).getBytes("UTF-8"); //字符串轉化為一個字節數組byte[]
Base64 encoder = new Base64();
String encodedStr = encoder.encodeToString(byteXl); //第一種 轉Base64
System.out.println("encodedStr is " + encodedStr);
String base64EncStr= new String (Base64.encodeBase64(byteXl),"UTF-8"); //第二種 轉Base64
System.out.println("base64EncStr is " + base64EncStr);
}catch(Exception e){
e.printStackTrace();
}
例:
    public static void main(String[] args) {

        Map<String,String> configMap = new HashMap<String,String>();
        configMap.put("merch","000001110100000812");
        configMap.put(
"path","F:/keypath/test_yufu_nonca.cer"); System.out.println("configMap is " + configMap); String jsonString = JSON.toJSONString(configMap); System.out.println("JSON.toJSONString(configMap) is " + jsonString); String json = JsonUtils.toJson(configMap); System.out.println("JsonUtils.toJson(configMap) is " + json); try{ byte byteXl[] = JSON.toJSONString(configMap).getBytes("UTF-8"); System.out.println("configMap is " + byteXl); Base64 encoder = new Base64(); String encodedStr = encoder.encodeToString(byteXl); System.out.println("encodedStr is " + encodedStr); String base64EncStr= new String (Base64.encodeBase64(byteXl),"UTF-8"); System.out.println("base64EncStr is " + base64EncStr); }catch(Exception e){ e.printStackTrace(); } }
結果:
configMap is {merch=000001110100000812, path=F:/keypath/test_yufu_nonca.cer}
JSON.toJSONString(configMap) is {"merch":"000001110100000812","path":"F:/keypath/test_yufu_nonca.cer"}
JsonUtils.toJson(configMap) is {"merch":"000001110100000812","path":"F:/keypath/test_yufu_nonca.cer"}
configMap is [B@7a36aefa
encodedStr is eyJtZXJjaCI6IjAwMDAwMTExMDEwMDAwMDgxMiIsInBhdGgiOiJGOi9rZXlwYXRoL3Rlc3RfeXVmdV9ub25jYS5jZXIifQ==
base64EncStr is eyJtZXJjaCI6IjAwMDAwMTExMDEwMDAwMDgxMiIsInBhdGgiOiJGOi9rZXlwYXRoL3Rlc3RfeXVmdV9ub25jYS5jZXIifQ==




 
 

Map<String,String>轉Json轉Base64