convert byte[] from json in android - android

how to get byte[] from json using webservice
My json format is given below:
[{"imgLogo":[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132,0,
9,6,7,20,19,18,20,20,19,20,21,22,20,22,20,21,24,22,21,21,20,20,20,20,20,
24,21,20,22,24,20,20,20,23,24,28,40,32,24,26,37,28,20,
21,33,49,33,37,41,43,46,46,46,23,31,51,56,51,44,55,40,45,46,43,1,10,10,10
,14,13,14,27,16,16,26,44,36,28,36,44,44,44,44,44,44,44,44,44,44,44,44,
44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,
44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,255,192,0,17,8,0,194,1,3,3....]}]
I'm using this following code to get json value like this :
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String imagestring = jsonChildNode.getString("imgLogo");
Now, I'm getting above byte[] value as string. then how can I convert this string to byte[]

first create a String containing your data and then
byte[] data = Base64.decode(iconString, Base64.DEFAULT);
now you have byte array containing your bitmap.

Use this getBytes method :
byte[] bytes = imagestring .getBytes("UTF-8");

Related

How to convert JSONObject directly to byteArray without intermediate toString?

I have a JSONObject returned from a library. Sometimes the JSONObjects are too big and they cause an exception when applying toString to the JSONObject before applying getBytes to convert it to a byteArray. How do I convert JSONObject to ByteArray directly?
Try this code
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
Json.createWriter(byteArray).write(jsonObject);
byte[] data = stream.toByteArray()
Sample:
JSONObject obj = new JSONObject();
obj.put("name", "foo");
obj.put("num", new Integer(100));
obj.put("balance", new Double(1000.21));
obj.put("is_vip", new Boolean(true));
obj.put("nickname",null);
Your solution :
obj.toString().getBytes(theCharset);

jackson ObjectMapper is adding an illegal character to base64 img string field on writeValueAsString

I am trying to send an object with an image converted to a base64 string, but when I convert that object to a json string to send it, multiple "\n" characters are added to that string which makes it invalid when it reaches the server.
Converting bitmap to a valid base64 (I checked if its valid)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
base64Str = Base64.encodeToString(outputStream.toByteArray(),
Base64.DEFAULT);
Creating the request and setting the base64 string to be sent to the backed
ObjectMapper mapper = new ObjectMapper();
Request = new Request();
payload.setData(base64);
on writeValueAsString a "/n" is added at the end of the base64 value which makes it invalid
String reqBody = "";
try {
reqBody = mapper.writeValueAsString(payload);
}
catch (Exception ex) {
}
a part of the valid base 64 before converting to json
"Kz7cruI+8gLNZRgnnKihQFDNt42sGaQlWMhVt2fLG9Q20NwpIx/
J0OWdOM4cso8tlZ3skldNWau0mmt7XT1P2/mcH
aWk15dFJrm+53Xndu7aP/9k="
a part of the valid base 64 after converting to json (notice the inclusion of '\n')
"Kz7cruI+8gLNZ\nRgnnKihQFDNt42sGaQlWMhVt2fLG9Q20NwpIx
/J0OWdOM4cso8tlZ3skldNWau0mmt7XT1P2/mcH\naWk15dFJrm+53Xndu7aP/9k=\n"
Found the issue, the new line character "\n" is added on encodeToString by default, to add a wrap effect. Just replace Base64.DEFAULT with Base64.NO_WRAP to remove it in the encoded string.

convert byteArray to string to initialize jsonObject

i have byteArray.
is it possible to convert byteArray to String?
please check my code
byte[] data = **some_byte_array**
JSONObject jsonObject = new JSONObject(data);
how do i fix this.
Try this
String decoded = new String(bytes, "UTF-8");
There are a bunch of encodings you can use, look at the Charset class in the Sun javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
answer credit goes to https://stackoverflow.com/a/1536365/4211264
Yes you can convert byte array to String using one of the String constructors like this :
String myString = new String(yourByteArray);
Documentation for the same:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])
All the best :)

convert a string got from Jsonobject to Byte[];

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;
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"));
As simple as :
byte[] byte = yourstring.getBytes();
byte[] byte = yourstring.getBytes(Charset.forName("UTF-8"));
String data = "sample text";
byte[] b = data.getBytes();
Use getBytes() method in string
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()
I was able to get the Bytearray from JSON using
pdfbytes = Base64.decode(response.getString("document"), Base64.DEFAULT) ;

How Can I change a string to the UTF-8 format in android

How Can I change a string to the UTF-8 format in android? For example when I get a text string from server I want change the text format to the UTF-8. How can I do this?
String getText = text; // this text variable has a value from the server and now I want change it to UTF-8 format.
String objects hold UTF-16 data internally. If what you want is to encode a String as UTF-8 for exporting, you need to convert it to a UTF-8 encoded byte[] array, such as with the String.getBytes(Charset charset) or String.getBytes(String charsetName) method, eg:
byte[] byteArray = text.getBytes(StandardCharsets.UTF_8);
byte[] byteArray = text.getBytes("UTF-8");
Hi You could use the String constructor with the charset parameter:
try
{
final String s = new String("AnyStringThatYouwant to convert", "UTF-8");
}
catch (UnsupportedEncodingException e)
{
Log.e("utf8", "conversion", e);
}

Categories

Resources