将从Jsonobject获取的字符串转换为Byte [];

时间:2015-10-02 04:50:48

标签: android

我正在从WEB API传输Bytearray的PDF,

我收到的回复如下

 {"document":"JVBERi0xLjMNCiXi48\/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpbmVzIDIgMCBSDQovUGFnZXMgMyAwIFINCj4+DQplbmRvYmoNCg0KMiAwIG9iag0KPDwNCi9UeXBlIC9PdXRsaW5lcw0KL0NvdW50IDANCj4+DQplbmRvYmoNCg0KMyAwIG9iag0KPDwNCi9UeXBlIC"}

我想将该字符串转换为Byte array []以呈现PDF;

请帮我解决问题;

5 个答案:

答案 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)

答案 4 :(得分:0)

我能够使用

从JSON获取Bytearray
  pdfbytes =   Base64.decode(response.getString("document"), Base64.DEFAULT) ;