How do I convert a JSONObject to a byte array and then convert this byte array to get back the original JSONObject?

Drew

I am using the AWS JSONObject class. Let's say I define a JSONObject object like so:

JSONObject obj = new JSONObject();
obj.put("Field1": 35);

JSONObject nestedObj = new JSONObject();
nestedObj.put("Name1":"value1");
nestedObj.put("Name2":42);

obj.put("Field2": nestedObj);

So the JSONObject looks like:

{"Field1": 35,
 "Field2": {"Name1": "value1",
            "Name2": 42}
}

I want to take this JSONObject and convert it to a byte array somehow:

byte[] objAsBytes = convertToBytes(obj);

where convertToBytes is some function that does this correctly. Then I would like to take this byte array and convert it back to the original JSONObject so it still preserves its original structure.

Does anyone know how to do this? I would like to do this because I am using Amazon Kinesis and more specifically the PutRecord API and a PutRecordRequest requires the data to be a ByteBuffer, so I need to convert the JSONObject to a byte array, and then wrap the byte array as a ByteBuffer. Then, when I retrieve the record I need to convert the ByteBuffer to a byte array and then get the original JSONObject.

user6188133

How about this?

byte[] objAsBytes = obj.toString().getBytes("UTF-8");

I used Json.simple to try it out, seems to work!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert byte array to io. stream and convert it back to byte array?

Convert a String to a byte array and then back to the original String

convert golang string format of a byte array back into original byte array

How do I convert a Java byte array into a Scala byte array?

How do I convert a byte slice of unknown size to a byte array?

How to Convert Byte Array back into string?

How do I convert a byte array back to an image consumable by a view in asp net core?

Convert JsonObject array to integer

How do I convert separate int values to hex byte array

How do I convert a WWWForm that will be a parameter for UploadHandlerRaw into a byte array?

What format is this, and how do I convert a byte array into this string format?

Convert byte array back to numpy array

In Java, how can I convert an InputStream into a byte array (byte[])?

How should I properly convert mp3 to byte array and then convert it back to mp3

Why byte array converted to string then convert back to byte array is not same?

Get byte array of string in .NET and convert back in Javascript

How to convert a Scala Array[Byte] to Java byte[]?

How to convert specific byte in a byte array to integer

How to convert a array of bits to a byte, (not to byte string)?

How do I convert [][]byte to []byte?

How to convert Byte array to ByteArrayOutputStream

How to convert byte array to Bitmap

How to convert outputStream to a byte array?

How to convert byte array to blob

How to convert array of byte into pdf?

How to convert float into byte array?

How to convert byte array to string

How to convert ImageSource to Byte array?

How to convert a byte array to pdf