简体   繁体   中英

Decode a Base64 String from JSONArray to Byte list in JAVA

I have a JSONArray that contains image files in Base64 format.

I would like to decode the images in Byte list.

My json array format:

jsonarray:[{"manchine_image":{"image_file":"VW5pdHlGUw...}

My existing code:


 List<String> image = new ArrayList<String>();   
                    for(int i = 0; i < jsonarray.length();i++) {

                          JSONObject jsonobject = jsonarray.getJSONObject(i);
                          image.add(new String(jsonobject.toString().getBytes("image_file")));
}

I tried this but i have this error:

java.io.UnsupportedEncodingException: image_file

Thanks anyway.

This is how you can decode a Base64 String to byte array:

import java.util.Base64;

class Scratch {
    public static void main(String[] args) {
        String imageFile = "VW5pdHlGUw...";
        byte[] decode = Base64.getDecoder().decode(imageFile);
    }
}

I found the answer. Thank you for your help.

image.add(new String(jsonobject.toString().getBytes("image_file"))); --> image.add(jsonobject.getJSONObject("machine_image").getString("image_file")); //correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM