在 JAVA 中将 Base64 字符串从 JSONArray 解码为 Byte 列表



我有一个包含Base64格式图像文件的JSONArray。

我想解码字节列表中的图像。

我的 json 数组格式:

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

我现有的代码:


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")));
}

我试过这个,但我有这个错误:

java.io.UnsupportedEncodingException: image_file

无论如何,谢谢。

这是将 Base64 字符串解码为字节数组的方法:

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

我找到了答案。谢谢你的帮助。

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

最新更新