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);
I 'm using following data to decode the UTF-8 encoded String.
Actual string: 秦世俊:用“心”工作 找到属于自己的成就感【开讲啦 20160430】T
UTF-8 encoded: 秦ä¸ä¿ï¼ç¨âå¿âå·¥ä½ æ¾å°å±äºèªå·±çæå°±æãå¼è®²å¦ 20160430ã"
. Output is same as input. What is the issue?
Method:
public String decodeString(String encodedString) {
return new String(encodedString.getBytes(), "UTF-8");
}
Just Use String result = URLDecoder.decode(string, "UTF-8");
Or use this
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
use this
String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded
and if it doesn't work for you
String decoded = new String(encoded.getBytes("ISO-8859-1"));
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 :)
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");
I need to decode JSONObject in Android 2.1 with Base64.I know that Base64 class supports Android 2.2+,that's why I include the source code in my project.So I need to do something like that :
JSONObject clientHash = new JSONObject();
byte[] tmpSecData = Base64.decode(clientHash.getJSONObject("client_auth_hash"));
Any suggestions how to do that or is it possible?
Lets try it,
Convert the clientHash.getJSONObject("client_auth_hash") in String then byteArray,
then use,
byte temp[];
Base64 b = new Base64();
String jsonString = clientHash.getJSONObject("client_auth_hash").toString();
temp = b.decode(jsonString.getBytes());
then use your temp byte[].
Hope this will help you. If its work then inform me. Thanx.