JSONArray to byte[] and vice versa


JSONArray byteArrayToJSON(byte[] byteArray) {
        JSONArray jsonArray= new JSONArray();
        for (byte byteValue : byteArray) {
            jsonArray.put(byteValue );
        }
        return jsonArray;
    }

byte[] jsonToByteArray(JSONArray jsonArray) throws JSONException {
        byte[] byteArray = new byte[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {
            byteArray[i] = (byte) jsonArray.getInt(i);
        }
        return byteArray;
    }

Reactions

Post a Comment

0 Comments