我正在从WEB API传输Bytearray的PDF,
我收到的回复如下
{"document":"JVBERi0xLjMNCiXi48\/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpbmVzIDIgMCBSDQovUGFnZXMgMyAwIFINCj4+DQplbmRvYmoNCg0KMiAwIG9iag0KPDwNCi9UeXBlIC9PdXRsaW5lcw0KL0NvdW50IDANCj4+DQplbmRvYmoNCg0KMyAwIG9iag0KPDwNCi9UeXBlIC"}
我想将该字符串转换为Byte array []以呈现PDF;
请帮我解决问题;
答案 0 :(得分:1)
试试这个
String json = "{\"document\":\"JVBERi0xLjMNCiXi48\/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpbmVzIDIgMCBSDQovUGFnZXMgMyAwIFINCj4+DQplbmRvYmoNCg0KMiAwIG9iag0KPDwNCi9UeXBlIC9PdXRsaW5lcw0KL0NvdW50IDANCj4+DQplbmRvYmoNCg0KMyAwIG9iag0KPDwNCi9UeXBlIC\"}";
JSONObject json_object = new JSONObject(json);
byte[] b = json_object.getString("document").getBytes();
//or
byte[] b = json_object.getString("document").getBytes(Charset.forName("UTF-8"));
答案 1 :(得分:0)
简单如下:
byte[] byte = yourstring.getBytes();
byte[] byte = yourstring.getBytes(Charset.forName("UTF-8"));
答案 2 :(得分:0)
String data = "sample text";
byte[] b = data.getBytes();
答案 3 :(得分:0)
在字符串
中使用getBytes()方法http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()
答案 4 :(得分:0)
我能够使用
从JSON获取Bytearray pdfbytes = Base64.decode(response.getString("document"), Base64.DEFAULT) ;