[英]Decode a Base64 String from JSONArray to Byte list in JAVA
我有一个 JSONArray,其中包含 Base64 格式的图像文件。
我想解码字节列表中的图像。
我的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.