简体   繁体   English

将 JSONArray 中的 Base64 字符串解码为 JAVA 中的字节列表

[英]Decode a Base64 String from JSONArray to Byte list in JAVA

I have a JSONArray that contains image files in Base64 format.我有一个 JSONArray,其中包含 Base64 格式的图像文件。

I would like to decode the images in Byte list.我想解码字节列表中的图像。

My json array format:我的 json 数组格式:

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:这是您如何将 Base64 字符串解码为字节数组的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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