Reputation: 153
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?
Upvotes: 1
Views: 505
Reputation: 106
Try this code
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
Json.createWriter(byteArray).write(jsonObject);
byte[] data = stream.toByteArray()
Upvotes: 2
Reputation: 43
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);
Upvotes: 0