Put/get byte array value using JSONObject

前端 未结 4 929
孤街浪徒
孤街浪徒 2021-01-13 17:03

I tried to get my byte[] value from JSONObject using following code but I am not getting original byte[] value.

JSONAr         


        
4条回答
  •  臣服心动
    2021-01-13 18:02

    This might be of help for those using Java 8. Make use of java.util.Base64.

    Encoding byte array to String :

    String encodedString = java.util.Base64.getEncoder().encodeToString(byteArray);
    JSONObject.put("encodedString",encodedString);
    

    Decode byte array from String :

    String encodedString = (String) JSONObject.get("encodedString");
    byte[] byteArray = java.util.Base64.getDecoder().decode(encodedString);
    

提交回复
热议问题