1

I am transmitting a PDF in Bytearray from WEB API,

I am getting the response as follows

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

I want to convert that string to Byte array[] for rendering the PDF;

Please Help me with problem;

Pragadees
  • 71
  • 1
  • 12
  • Possible duplicate of [How to convert Java String into byte\[\]?](http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte) – karan Oct 02 '15 at 04:55

5 Answers5

1

Try this

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"));
NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
0

As simple as :

byte[] byte = yourstring.getBytes();
byte[] byte = yourstring.getBytes(Charset.forName("UTF-8"));
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0
    String data = "sample text";
    byte[] b = data.getBytes();
Akash Singh
  • 748
  • 1
  • 6
  • 14
0

Use getBytes() method in string

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()

subhash
  • 2,689
  • 18
  • 13
0

I was able to get the Bytearray from JSON using

  pdfbytes =   Base64.decode(response.getString("document"), Base64.DEFAULT) ;
Pragadees
  • 71
  • 1
  • 12